Is there a reason to cache myId like below (let’s say the object myId will only be used for this popup function, nowhere else in the JavaScript).
var myId = document.getElementById('myId');
myId.onclick = function () {
alert('button was clicked');
}
I figure that the onclick event will only be set once and that it is unnecessary. But I’m not sure.
The only reason to cache would be if you would want to use the
myidelement for more than just binding the click event.One often caches elements for just that reason, which saves performance because you dont have to search the DOM tree more than once.