1

Many years ago when I studied JavaScript, that I later almost did not use, I learned that I could use element id in place of its name. E.g.:

if we have code like this:

<button type="button" id="addButton">Add</button>

we can use the element two ways:

addButton.addEventListener('click', function(){
  console.log('click');
});

or

document.getElementById('addButton').addEventListener('click', function(){
  console.log('click');
});

The former version looks more convenient to me, but I found that nobody uses it. Why? What's the difference?

  • 1
    This is a well posed question, but bobince and others address the issue in more full depth than would be produced in an answer here. – Travis J Oct 28 '15 at 23:11
  • Duplicate of http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables ? – Webbies Oct 28 '15 at 23:12
  • @Webbies, I did not find it, sorry. Thanks for pointing out. –  Oct 28 '15 at 23:26

0 Answers0