|
-
April 20th, 2007, 09:10 AM
#1
getElementById and Array
i'm using getElementById() function to get the <img> in the document.... these <img> are the array of the images.... while using the getElementById() in javascript, it always returns 'null'
html code
Code:
Ex... <img name=sameName> <img name=sameName> <img name=samename>
I think by keeping the same name <img> will be treated as array of images...
using Javascript code,
Code:
var obj;
var iTotalImg = ...; // this is calculated
for (i = 0; i <iTotalImg; i++)
{
obj = document.getElementById("sameName[" + i + "]");
obj.src = imgList[i];
//obj will always be 'null'
}
always, 'obj' object is 'null'... what is wrong in my code...??
Cheers!
VB
Venu Bharadwaj
"Dream it. U can do it!"
-
April 20th, 2007, 10:12 AM
#2
Re: getElementById and Array
the function name is ById, and you're only setting the name attribute. It doesn't work that way.
An ID has to be wholly unique in a document. It's the unique identifier. You can have multiple elements share the same name, though, and you can use getElementsByName instead, though I'm not sure what the support level is for that function; I know getElementById is pretty much universal. An alternative solution is to give those elements a class name and use an implementation (note: not a standard method) getElementsByClassName.
-
April 20th, 2007, 12:45 PM
#3
Re: getElementById and Array
Eli is correct. You need to use getElementsByName(). Note that this already returns an array for you.
Code:
var total_obj = document.getElementsByName("sameName");
for(current_obj=0;current_obj<total_obj.length;current_obj++){
var obj = total_obj[current_obj];
obj.src = imgList[i];
}
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|