I'm trying to use a variable to hold an HTML id so I don't have to keep writing getElementById over and over again. But I can't get this to work. Here's my code:
var box1 = document.getElementById('box1');
function slide4(){
box1.style.width='10%';
}
It works if I put the variable inside the function, but that seems like bad practice as I want to access that variable in other functions and don't want to keep declaring the variable over and over again.
function slide4(){
var box1 = document.getElementById('box1');
box1.style.width='10%';
}
Any ideas?