Click to See Complete Forum and Search --> : getElementsByName question


kapot
03-27-2003, 12:15 AM
I have <img> tag like this :

<img name="thumb1" src="test.gif" border=0></img>

Why I can not get that object by using getElementsByName?

I tried this way :

var obj = document.getElementsByName("thumb1");
obj.src = "newpic.gif";

But, I can do it like this :

var obj = document.all["thumb1"];
obj.src = "newpic.gif";

So, when should we use getElementsByName ?

I use MSIE 6.0 and Dreamweaver MX.

Please enlighten me :)

Thanks.

khalidali63
03-27-2003, 12:25 AM
document.getElementsByName("name")

returns an array of type NodeList.to get access to an individual element you will have to access it using array index value.Say you wanted to access the first image element

var img = document.getElementsByName("thumb1")[0]

and then you access all of the attributes of the image element as normal
alert(img.src)

Cheers

Khalid