0
How can I change background-image with input in JavaScript?
Image JavaScript
8 Réponses
+ 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.
+ 1
element.style.backgroundImage='imgurl';
or with bracket notation
element['style']['background-image] ='imgurl';
What is the input btw?
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>
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
0
Szymon ,
document not dokument
0
Omkar,
I'm correct 'document' but it didn't help
0
Szymon ,
what error are you getting?
0
Error: Padding error: The keyword 'const' is reserved .