...

View Full Version : images have the same name value



mehere8
08-27-2010, 08:13 AM
if the elements have name attributes with the same value, according to legacy DOM, it will return an array of those elements if you access the name property of 'document'.

However, this doesn't apply to img elements. If the HTML code is as follows:


<img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
<form name="pic">a</form>
<form name="pic">b</form>

then
document.forms["pic"] (return an array) <-- as expected
document["pic"] or document.pic (return an array) <-- as expected
document.images["pic"] :
in IE -- it returns the last image
in FF -- it returns the first image
~~OMG~~

any idea?

thx in advance.

Arty Effem
08-27-2010, 10:03 AM
AFAIK that applies only to forms and their elements.
document.images[ 'duplicate' ] can be interpreted any way, so there is no unexpected behaviour there.

vwphillips
08-27-2010, 10:06 AM
<!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" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
<form name="pic">a</form>
<form name="pic">b</form>
<script type="text/javascript">
/*<![CDATA[*/
alert(document.getElementsByName('pic').length);
/*]]>*/
</script></body>

</html>

mehere8
08-27-2010, 02:44 PM
document.images[ 'duplicate' ] can be interpreted any way, so there is no unexpected behaviour there.

but it should return an array of those images instead of one single image~~~~


@vwphillips haha this can be a workaround but still need to be filtered with 'img' tag coz i want images with that name.

Arty Effem
08-27-2010, 03:58 PM
but it should return an array of those images instead of one single image~~~~Is there a specification stipulating that?
Do you know that the name attribute is deprecated except for elements of forms?

vwphillips
08-27-2010, 04:59 PM
document.getElementsByTagName( 'img' ).getElementsByName( 'pic' );

will result in an error

use post#3

or

<!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" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<img src="img/a.jpg" name="pic" /><img src="img/b.jpg" name="pic" />
<form name="pic">a</form>
<form name="pic">b</form>
<script type="text/javascript">
/*<![CDATA[*/
var imgs=document.getElementsByTagName( 'img' )
for (var ary=[],z0=0;z0<imgs.length;z0++){
if (imgs[z0].name&&imgs[z0].name=='pic'&&imgs[z0].nodeName.toUpperCase()=='IMG'){
ary.push(imgs[z0]);
}
}
alert(ary.length);
/*]]>*/
</script></body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum