javascript - Does getElementByid return a NodeList? -


i know simple question, must start somewhere..

i haven't been able track down answer here am.

document.getelementsbyclassname(); or document.getelementsbytagname();

return nodelists.

meaning if want access element node must access using index referring specific node in nodelist

for example:

html

<div id="div1"> <p> text in here</p> </div>

<div id="div2"> <p> text in here</p> </div>

<div id="div3"> <p> text in here</p> </div>

javascript

var x = document.getelementsbytagname("div")[0];

//returns div1

now can apply properties , methods object node.

however, if use

document.getelementbyid("div1");

this not return nodelist single cell.

bur rather returns object node div1 itself!

thus no need document.getelementbyid("div1")[0];

is correct?

yes, right. return value reference element object, or null if element specified id not in document.

and why? because id colud appear 1 or 0 times in dcument, there 1 (if id exists) element return.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -