I'm looking to update my website background using JavaScript, starting with buttons before moving to a drop-down menu. I have the following code that successfully changes the background when the button is clicked:
function selectText(test){
var arr = document.getElementById(test).value;
if (arr == 1){
document.body.style.backgroundImage = "url('https://images6.alphacoders.com/431/thumb-1920-431411.jpg')";
}
else if (arr == 2){
document.body.style.background = 'none';
}
}
<button id="btnim" value="1" onclick="selectText(this.id)">text</button>
<button id="btnim2" value="2" onclick="selectText(this.id)">more text</button>
However, when I change the condition in the else if
statement to arr == 2
and both buttons have the same id (btnim
), it doesn't work as expected. I must be overlooking something simple. Can someone please point out what I am doing wrong?