Results 1 to 8 of 8

Thread: type="image" is KILLING ME!

  1. #1
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry type="image" is KILLING ME!

    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.

    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.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    document.images finds occurences of the <img> tag.
    Code:
    var tg = document.getElementsByTagName("*");
    for (i=0;i<tg.length;i++) {
    	if (tg[i].type == "image") {
    		tg[i].setAttribute("alt", "poof");
    		}
    	}
    - Mike

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    poof? Anyways, you should formally declare i.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

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

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It is part of the <input> tag not the image tag, and that is why it is excluded from document.images.
    - Mike

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am weeping with appreciation. You guys are great. Many, Many thanks. Now I can sleep tonight ;-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •