I have a HTML code below
var div1=document.querySelector("#div");
function p1(){ div1.style.display="none";alert("p1");}
<script src="o.js"></script>
<button onclick="p1();">p1</button>
<div id="div" style="display:block;">div1</div>
It is not working at all because global var div1
is not updated I guess.
if I put var div1
into function p1
, it works.
However, I want to create a lot of global variables, so I don't need to repeat a lot in every other function.