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