By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
--
jeremy
Oct 2 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jeremy wrote:
What code can I add to an "onclick" in order to determine which
occurrence of the checkbox has just been clicked?
<input type="checkbox" name="ch_fld" onclick="callfunc(this)">

Now your callfunc function will be passed a reference to the checkbox that
has been clicked.
http://www.javascripttoolbox.com/bestpractices/#forms

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 2 '06 #2

P: n/a
In article <ef*********@news3.newsguy.com>, Matt Kruse says...
Jeremy wrote:
What code can I add to an "onclick" in order to determine which
occurrence of the checkbox has just been clicked?

<input type="checkbox" name="ch_fld" onclick="callfunc(this)">

Now your callfunc function will be passed a reference to the checkbox that
has been clicked.
http://www.javascripttoolbox.com/bestpractices/#forms
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)?

--

jeremy
Oct 2 '06 #3

P: n/a
Jeremy scribed:
>In article <ef*********@news3.newsguy.com>, Matt Kruse says...
>Jeremy wrote:
What code can I add to an "onclick" in order to determine which
occurrence of the checkbox has just been clicked?

<input type="checkbox" name="ch_fld" onclick="callfunc(this)">

Now your callfunc function will be passed a reference to the checkbox that
has been clicked.
http://www.javascripttoolbox.com/bestpractices/#forms

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[i].checked) {indexValue = i;}
}
return indexVal; //Or do what you want with indexValue here.
}
--
Ed Jay (remove 'M' to respond by email)
Oct 2 '06 #4

P: n/a
In article <ce********************************@4ax.com>, Ed Jay says...
Jeremy scribed:
In article <ef*********@news3.newsguy.com>, Matt Kruse says...
Jeremy wrote:
What code can I add to an "onclick" in order to determine which
occurrence of the checkbox has just been clicked?

<input type="checkbox" name="ch_fld" onclick="callfunc(this)">

Now your callfunc function will be passed a reference to the checkbox that
has been clicked.
http://www.javascripttoolbox.com/bestpractices/#forms
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[i].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?

--

jeremy
Oct 2 '06 #5

P: n/a
Ed Jay wrote:
function getIndex (what) {
var indexValue = 0;
for(var i=0; i < what.length; i++) {
if(what[i].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[i]==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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 2 '06 #6

P: n/a
In article <ef*********@news3.newsguy.com>, Matt Kruse says...
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.

--

jeremy
Oct 5 '06 #7

P: n/a
Jeremy wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 5 '06 #8

P: n/a
In article <eg*********@news3.newsguy.com>, Matt Kruse says...
Jeremy wrote:
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

--
jeremy
Oct 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.