Can someone please explain why my code is not working !!
When I get the element By Id it works perfectly fine. But the same method with getElementsByTagName() does not.
As you can clearly see document,getElementsByTagName returns an array of elements, not a single element.
document,getElementsByTagName
So you have to follow proper indexing otherwise it will throw an exception as in your case.