checkbox (I guess any input field really) arrays - how to identify the "current" entry?

J

Jeremy

If we have a table with say 3 rows in it and one of the columns contains
a checkbox such as

<table>
<tr>
<td><input type="checkbox" name="ch_fld"></td>
</tr>
<tr>
<td><input type="checkbox" name="ch_fld"></td>
</tr>
<tr>
<td><input type="checkbox" name="ch_fld"></td>
</tr>
</table>

What code can I add to an "onclick" in order to determine which
occurrence of the checkbox has just been clicked?
 
J

Jeremy

Jeremy scribed:
Perhaps I dodn't make the question quite clear - I am seeking to know
the "index" of the item clicked - is it 0,1 or 2 (pertaining to my
suggested example in which there were 3 entries of the same name)?

Perhaps this will work?

function getIndex (what) {
var indexValue = 0;
for(var i=0; i < what.length; i++) {
if(what.checked) {indexValue = i;}
}
return indexVal; //Or do what you want with indexValue here.
}

Now, if I am reading that right, that will return me every occurrence
that has been checked - what I would like to know is which occurrence
did the user just click?
 
M

Matt Kruse

Ed said:
function getIndex (what) {
var indexValue = 0;
for(var i=0; i < what.length; i++) {
if(what.checked) {indexValue = i;}
}
return indexVal; //Or do what you want with indexValue here.
}


Or:

function getIndex(obj) {
var indexValue = 0;
var els = obj.form.elements[obj.name];
for(var i=0; i < els.length; i++) {
if(els==obj) {return i;}
}
return -1;
}
(untested)

You would want to add some error-checking in there in case there is only 1
checkbox in the group, for example.

Also, you may want to consider the need to finding the index to begin with.
There may be a better way to accomplish what you are ultimately trying to
do.
 
J

Jeremy

Also, you may want to consider the need to finding the index to begin with.
There may be a better way to accomplish what you are ultimately trying to
do.

Thanks for your help with this. The need is to be able to have a grid
(table) of variable length with a variable number of fields in it and,
upon submission of the form, be able to identify those "rows" where the
data has changed. The problem encountered pertained to checkboxes, which
are only submitted when they are checked and hence when a checkbox was
checked/unchecked, wanted to set a corresponding hidden field [indexed]
to a Y or N value.
 
M

Matt Kruse

Jeremy said:
when a checkbox was checked/unchecked, wanted to set a corresponding
hidden field [indexed] to a Y or N value.

Instead, you could keep the hidden field in the same table cell.
When the checkbox is clicked, find the TD containing it, then find the
hidden field within the TD with the name you want, and use it. There is no
need to go up to the form level and try to find things that way.
 
J

Jeremy

Jeremy said:
when a checkbox was checked/unchecked, wanted to set a corresponding
hidden field [indexed] to a Y or N value.

Instead, you could keep the hidden field in the same table cell.
When the checkbox is clicked, find the TD containing it, then find the
hidden field within the TD with the name you want, and use it. There is no
need to go up to the form level and try to find things that way.

OK thanks I may look into that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top