473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
8 1702
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ben | last post by:
Hi all, i have file input field in a form like this: <input name="isbn" type="text" size="25" value="<?php echo $_POST ?>" /> <input name="image" type="file" size="25" value="<?php echo...
4
by: Paul Jørstad | last post by:
Hello! In a form I have an input field. It's supposed to get input form a scanning device. Thus, the input might contain special characters (like the Group Separator in a EAN barcode). Now, I...
2
by: Kai Grossjohann | last post by:
I would like to put a text input field (in the sense of <input type="text">) and an image next to each other, where I know the size in pixels of the image, and I know the total width in em. I...
5
by: Clive Backham | last post by:
I'm having trouble with Instant Payment Notification on PayPal. One of the forms that they generate, which invokes one of my scripts, has a submit button with a blank name. The HTML fragment is...
3
by: Chris S | last post by:
I've been unsuccessfully attempting to write a method in Javascript to change an input field from TEXT to SELECT when a user selects (or unselects) a radio button. In my frustration I have been...
3
by: Adam Smith | last post by:
Hello, How does one adjust the input field size (height) in a form, say to accept 10pt rather than the 12pt txt that apparently is the norm. Thanks --Adam--
6
by: George Slamowitz | last post by:
Hello all I have a HTML control generated by the following: INPUT TYPE="hidden" NAME="MyAnswer" VALUE="" Is there a way to reference the Value of this control using VB program code??? ...
13
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.