Click to See Complete Forum and Search --> : How do I select specific dynamic form element in JavaScript


Pailin
06-12-2005, 10:14 AM
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

Pailin
06-12-2005, 06:15 PM
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):


<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 :confused: