Does document.getElementById return a live dom element?

Does document.getElementById return a returned DOM object in JavaScript? I'm interested in knowing the reason for the performance

+6
source share
1 answer

The distinction between standard and live is usually used for item lists. document.getElementById returns a reference to a single object on the DOM node. After receiving the node, the link will always point to the same node.

HTML for example:
 <div id="foo"></div> 
JS for example:
 var foo, bar; foo = document.getElementById('foo'); //gets the div bar = document.getElementById('bar'); //null foo.setAttribute('id', 'bar'); console.log(foo.id); //'bar' console.log(bar.id); //TypeError 

Links are not updated because the identifier of an element can be changed.

This contrasts with something like document.getElementsByTagName , which returns a list of elements with the specified tag. The list will be automatically updated when items are added or removed from the DOM.

+12
source

Source: https://habr.com/ru/post/955129/


All Articles