Reputation: 10017
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
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