javascript - getElementByTagName question -


i know if want find group of elements, getelementsbytagname method , returns nodelist. if looking tag name "body" , why need add [0] after ("body") element? there 1 body tag in html document.

 var body = document.getelementsbytagname("body")[0];  body.classname = "unreadable"; 

why cant write code without index[0] this

 var body = document.getelementsbytagname("body");  body.classname = "unreadable"; 

if write code class unreadable not added body tag ...why?

because document.getelementsbytagname allways returns nodelist. if want find easier way retrieve body can use document.body


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -