STEEL
STEEL

Reputation: 10017

getElementsByTagName is not working Simple JS

another quesion: http://jsfiddle.net/ajinkyax/qGzTY/1/ Above link shows a js calculator, but whn u click nothign happens

Im just amazed why this simple function not working!!!.

I even tested with a tag, still it wont wokr. getElementsByTagName("a")

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>documentElement</title>
    </head>
    <body>
         <p>This is 4rth child (3)</p>
        <p>This is 4rth child (3)</p>

        <p>This is 5th child (4) <span id="some">CHANGE THIS</span></p>

        <script type="text/javascript">
            var mySpan = document.getElementsByTagName('span');

            mySpan.innerHTML = 'This is should change';

        </script>
    </body>
</html>

Upvotes: 0

Views: 1364

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382112

Do this :

 var mySpan = document.getElementsByTagName('span')[0];
 mySpan.innerHTML = 'This is should change';

getElementsByTagName doesn't return an element but a collection of all elements having this tag name. If you want only the first one, add [0].

As was pointed by user1689607, if you want to change just this specific span, you'd better do

 var mySpan = document.getElementById('some');

Upvotes: 5

Related Questions