I was wondering why I can't use global variables with HTML elements.
I'm trying to do something like this:
<script>
//I want to declare my variables here
var div1 = document.getElementById('a');
var div2 = document.getElementById('b');
var div3 = document.getElementById('c');
//And I want to use them here, inside a function.
function myFunction(){
div1.style.display = 'none';
div2.style.display = 'none';
div3.style.display = 'none';
}
</script>
Somehow, it doesn't let me use the variables I have declared outside of the function, but if I do like this, it does let me:
<script>
function myFunction(){
var div1 = document.getElementById('a');
var div2 = document.getElementById('b');
var div3 = document.getElementById('c');
div1.style.display = 'none';
div2.style.display = 'none';
div3.style.display = 'none';
}
</script>
Thing is, I have a lot of similar functions and I dont want to declare the variables each time in the functions I make, Is there a way to assign global variables to HTML elements, so that way I can achieve what I want as in the first snippet?
Thanks!