PDA

View Full Version : type="image" is KILLING ME!



summergs
03-01-2007, 01:45 AM
Hello,

I wrote a script to loop through all objects on the page and store them into an Array..... for(i=0; i<document.forms[0].elements.length; i++)

I also captured the images....for(i=0;i<document.images.length;i++)

Tested my code ONLY to find that my array did NOT contain any <input type="image"> objects.:eek:

No matter what I do, I am not able to detect any <input type="image"> objects!!!!!!!!

Advice anyone? Code examples would be great!

Thanks in advance.

mburt
03-01-2007, 01:50 AM
document.images finds occurences of the <img> tag.

var tg = document.getElementsByTagName("*");
for (i=0;i<tg.length;i++) {
if (tg[i].type == "image") {
tg[i].setAttribute("alt", "poof");
}
}

jscheuer1
03-01-2007, 06:12 AM
poof? Anyways, you should formally declare i.

mburt
03-01-2007, 11:03 AM
Yeah... I guess:

var tg = document.getElementsByTagName("*");
for (var i=0;i<tg.length;i++) {
if (tg[i].type == "image") {
tg[i].setAttribute("alt", "poof");
}
}

codeexploiter
03-01-2007, 11:33 AM
I don't think even using the elements collection that usually used to handle the form elements you can't access the input type="image" objects.

input type=image objects are excluded from the elements collection due to some reasons.

mburt
03-01-2007, 11:39 AM
It is part of the <input> tag not the image tag, and that is why it is excluded from document.images.

jscheuer1
03-01-2007, 01:16 PM
Yes, you can access the input element the same as any element and, if it is a part of a form, the same as any form element. The tricky part and this just occurred to me looking at this a second time is that not all elements and not all form elements have type attributes so you probably need to use:


if (tg[i].type&&tg[i].type == "image")

to prevent an error in cases where you have a tag/object that has no type attribute.

summergs
03-01-2007, 02:37 PM
I am weeping with appreciation. You guys are great. Many, Many thanks. Now I can sleep tonight ;-)