0

How can I change background-image with input in JavaScript?

Image JavaScript

9th Oct 2020, 5:16 AM
Szymon
Szymon - avatar
8 Respostas
+ 1
Szymon , >But I want change image if I change number in <input> then don't use `onclick` on button. use `oninput` on <input> element. <input type="number" id="x5" oninput="changeImg(this)"/> Note I'm passing `this` to changeImg(). inside function it's first parameter will refer to input element. function changeImg(input){ console.log(input.value); } You can set background-image as I have already shown in previous answer.
9th Oct 2020, 6:23 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
element.style.backgroundImage='imgurl'; or with bracket notation element['style']['background-image] ='imgurl'; What is the input btw?
9th Oct 2020, 5:25 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
I want bulid somethink like that: <input type="number" id="x5"/> <button onclick="changeImage();"> Change Image </button> But I want change image if I change number in <input>
9th Oct 2020, 5:07 PM
Szymon
Szymon - avatar
0
HTML <body id="body"> <input type="number" id="x5" input="changeImg(this)"/> </body> JS function changeImg(){ const x5 =dokument.getElementById('x5'); const body = =dokument.getElementById('body'); If(x5>5){ body.style.backgroundImg = ('http://cdn.pixbay.com......jpg'); } But it still doesn't work
10th Oct 2020, 11:17 AM
Szymon
Szymon - avatar
0
Szymon , document not dokument
11th Oct 2020, 6:25 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Omkar, I'm correct 'document' but it didn't help
11th Oct 2020, 9:28 AM
Szymon
Szymon - avatar
0
Szymon , what error are you getting?
11th Oct 2020, 9:37 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Error: Padding error: The keyword 'const' is reserved .
11th Oct 2020, 10:29 AM
Szymon
Szymon - avatar