javascript - getElementsByTagName does not seem to work -
can please explain why code not working !!
when element id works fine. same method getelementsbytagname() not.
also if use queryselector(), works. if use queryselectorall() same error returns.
test.html:15 uncaught typeerror: cannot set property 'color' of undefined
here code:
<doctype! html> <html> <head> </head> <body> <h1>hello world</h1> <p id="par">hello world</p> <script> var par = document.getelementbyid('par'); par.style.color = "red" var heading = document.getelementsbytagname("h1"); heading.style.color = "red" </script> </body> </html>
as can see document,getelementsbytagname
returns array of elements, not single element.
so have follow proper indexing otherwise throw exception in case.
Comments
Post a Comment