Question

Asked on 2011-05-09 at 04:53:52ID: 27360736

JavaScript: getElementsByTagName returns extra stuff

by: hankknight500

Rating7.8Very Good

Why do I get more than 3 results for tags matching 'a'?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Website</title>
</head>
<body>

<h1>Hello World</h1>

<p>
 <a>Hello</a>
 <a>World</a>
 <a>Testing</a>
</p>

<script type="text/javascript">

var items = document.body.getElementsByTagName("a");
for(var ii in items) {
    alert((ii) +': '+ items[ii].innerHTML);
}

</script>

</body>
</html>
                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:

Select allOpen in new window

Related Solutions

Solution

Sign up now to view this solution! It's quick, easy, and secure to subscribe. We will return you to this solution, unlocked, when you’re done.

About this Solution

View This Solution - Start Your 30 Day Free Trial
Zone
general
Tags
Participating Experts
4
Solution Grade
A

Replies

 by: pratima_mcs

Posted on 2011-05-09 at 05:00:38ID: 36812467

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: chaituu

Posted on 2011-05-09 at 05:07:17ID: 36812468

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: Kravimir

Posted on 2011-05-09 at 05:15:28ID: 36812469

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

 by: leakim971

Posted on 2011-05-09 at 05:51:01ID: 36812470

All comments and solutions are available to Premium Service Members only. Sign up to view the solution to this question. Already a member? Log in to view this solution.

Log In

Forgot your password?Sign up

Top Experts

  1. mplungjan

    1,840

    0 points yesterday

    Profile

BrowseAre you an Expert?