How do I select specific dynamic form element in JavaScript

Sharky Forums


Results 1 to 2 of 2

Thread: How do I select specific dynamic form element in JavaScript

  1. #1
    Tiger Shark Pailin's Avatar
    Join Date
    Sep 2000
    Location
    East Grinstead, UK
    Posts
    621

    How do I select a specific dynamic form element in JavaScript

    In particular an <img>, which I don't think is considered an element, as if I do:

    var el = document.frmSuppMessage.elements;
    for(var i = 0 ; i < el.length ; ++i) {
    alert("El name ="+ el[i].name +".");
    }

    <img ...> tags do not show up?

    so I can't go through all elements and do a compare as I've done in the past :/

    Main point:

    The tag in question:

    <img SRC="../images/+.gif" name="img<%=rsOrd.Fields("Part_No")%>" onclick="this.src=hideRows('<%=rsOrd.Fields("Part_No")%>')">

    if I specify the exact name:
    alert("this="+ document.imgECL250.src + ".");

    gets me:
    this=http://SERVER/SITE/images/+.gif

    What I want to do is something like:

    function hideRows(rowIDs)
    {
    var imgToMod;
    imgToMod = "Buy" + rowIDs

    alert("this="+ document.imgToMod.src + ".");
    }

    which of course gets me:
    Error:'document.imgToMod.src' is null or not an object

    But I think you get what I'm trying to do...

    I can think of some sloppy hidden text input ways round this problem, but would love to know the smart way.

    Cheers, Pailin
    Last edited by Pailin; 06-12-2005 at 06:27 PM.
    Appreciate what you've got, but don’t dwell on it

    DFI LanParty UT RDX200CF-DR Crossfire
    Opteron 144 @ 2520MHz 1.6vcore
    pc4000 G.Skill Extreme Series Dual Channel 2GigKit
    3,4,4,8 1t
    Asus 8800GT
    4x HDD - 1.3TB
    Seasonic S12 500W Silent

  2. #2
    Tiger Shark Pailin's Avatar
    Join Date
    Sep 2000
    Location
    East Grinstead, UK
    Posts
    621
    Found an answer elsewhere, so here it is:

    Basically what I was trying to do was reference a "+" or "-" image of a particular row in a table which is clicked on to expand or collapse a row(s) in a table & change image.

    Obviously the "this" points at the correct image, but I needed which image it was so as to make the correct action to the corresponding row(s) in the table.

    The important part you helped me with was simply being able to change the row property, having id'd it with a combined text + var (see bold lines):

    Code:
    <script>
    function hideRows(rowIDs)
    {	
    	var imgmod;
    
    	imgID = "img" + rowIDs
    
    	var objImg = document.getElementById(imgID);
    	var objRow = document.getElementById(rowIDs);
    	
    	var whatsInStr;
    	whatsInStr = InStr(objImg.src, "+")
    	if (whatsInStr >= 0){
    		objRow.style.display="";
    		return "../images/-.gif";
    	}
    	if (whatsInStr < 0){
    		objRow.style.display="none";
    		return "../images/+.gif";
    	}
    }
    </script>
    
    <img SRC="../images/+.gif" id="img<%=rsOrd.Fields("Part_No")%>" onclick="this.src=hideRows('<%=rsOrd.Fields("Part_No")%>')">
    Now all I have to do is sort out my customers DB with it's duplicate Part No's and Part No's with leading spaces O.o
    Last edited by Pailin; 06-12-2005 at 06:22 PM.
    Appreciate what you've got, but don’t dwell on it

    DFI LanParty UT RDX200CF-DR Crossfire
    Opteron 144 @ 2520MHz 1.6vcore
    pc4000 G.Skill Extreme Series Dual Channel 2GigKit
    3,4,4,8 1t
    Asus 8800GT
    4x HDD - 1.3TB
    Seasonic S12 500W Silent

Posting Permissions

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