Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  172] [ 4]  / answers: 1 / hits: 18687  / 13 Years ago, thu, july 21, 2011, 12:00:00

I know that if we want to find a group of elements, getElementsByTagName is the method for us and it returns a NodeList. but if we are looking for tag name with body , then why we need to add [0] after the (body) element? There is only one body tag in an HTML document.



 var body = document.getElementsByTagName(body)[0];
body.className = unreadable;


why we cant write this code without index[0] like this



 var body = document.getElementsByTagName(body);
body.className = unreadable;


If i write this code the class unreadable will not be added with body tag ...why?


More From » html

 Answers
89

Because document.getElementsByTagName allways returns NodeList. If you want find easier way to retrieve body you can use just document.body


[#91086] Tuesday, July 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;