Javascript, variable is null - cannot call method 'getElementsByTagName' of null -
i have basic javascript i'm working around with. works fine on localhost throws error when implemented on web. following error in dev tools using firefox or crome.
uncaught typeerror: cannot call method 'getelementsbytagname' of null.
any appreciated. thanks
<!doctype html> <html> <head> <title>title</title> <style type="text/css"> body { background-color: beige; margin: 0; } #main li { width: 100px; padding: 5px 0; } </style> </head> <body> <div id="main"> <ul> <li>a list item</li> <li>b list item</li> <li>c list item</li> </ul> </div> <script type="text/javascript"> var insert = document.getelementbyid("main"); insert.getelementsbytagname("li")[1].style.borderright = "1px solid blue"; </script> </body> </html>
the above code looks good. there no errors. possible condition can't work may have forgotten change original id name using in project demo id i.e main.
it hard say. check doing following
- change variable name i.e
insert. - keep javascript in script tags
<script></script>. - or there hidden characters.
you enclosing html tag caps i.e html. suppose might problem.
Comments
Post a Comment