View Full Version : getElementsByTagName,<a>, returns href instef of object ?

02-25-2007, 09:19 AM
can't correct title, so if admin have some time , please change insef to instead.

<DIV id = "mi1">
<a class = "mi" href="javascript:alert('a');">java script </a>
<a class = "mi" href="#n">aaaaaaaaaaaaaa </a>
<a class = "mi" href="#n">ffffffffffff </a>

theThing = document.getElementById('mi1');
theThingChilds = theThing.getElementsByTagName("a");
for (i = 0; i <= theThingChilds.length-1; i++)
// first element alerts "javascript:alert('a');", second ... ??? }

Thanks for help.

Bill Posters
02-25-2007, 09:51 AM

theThing = document.getElementById('mi1');
theThingChilds = theThing.getElementsByTagName('a');
for (i=0; i<theThingChilds.length; i++) {
alert(theThingChilds[i].firstChild.nodeValue); // [node object] [node value]

02-25-2007, 10:01 AM
To explain again: I want FF:"[ObjectHTMLAElement]", IE: "[Object]" to be returned in alert box.
Whay this does not work in my code ? It works if I do it with DIV element or some other element.

there is complet sample that returns wrong result:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html >
<head >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<body >
<DIV id = "mi1" >
<!-- tag a coz this is only element that takkes css a:active, a:focus-->
<a href="#n">hhhhhhhhhhhhm </a>
<a href="#n">hkgffhgjkgfh </a>
<a href="#n">fhgjkhgjkl </a>


<script language="javascript" type="text/javascript">
theThing = document.getElementById('mi1');
theThingChilds = theThing.getElementsByTagName('a');
for (i = 0; i <= theThingChilds.length-1; i++)

02-25-2007, 12:08 PM
what is being returned is an HTML-object, it just seems that browsers treat them differently when using alert. You can still access its properties, as with a div or anything else, why do you need it to alert something nonsensical?

02-25-2007, 01:51 PM
Seems you are right. Was hunting some other error, and come up with this, and things got even more complicated.
Note: document.write produces same thing as alert. And it is not browser thing as far as I know(IE and FF works the same)

Thanks for tip.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum