473,242 Members | 1,473 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,242 software developers and data experts.

id of checked checkbox

how can I read (in alert for example) array index number of checked
checkbox?

I have:

<input type="checkbox" id="id_number[]" name="check[]" value="1"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="2"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="3"
onclick="show()"/>

function show()
{
alert(here: display clicked id or name number of index? (for value "3" i
should be 2))
}

sorry for my english :)
Jul 23 '05 #1
4 26933
"Piotr" <pi**@gaztea.pl> wrote in message
news:5j****************************@40tude.net...
how can I read (in alert for example) array index number of checked
checkbox?

I have:

<input type="checkbox" id="id_number[]" name="check[]" value="1"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="2"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="3"
onclick="show()"/>

function show()
{
alert(here: display clicked id or name number of index? (for value "3" i
should be 2))
}

sorry for my english :)


Is this what you want?

<html>
<head>
<title>check_id.htm</title>
<script type="text/javascript">
function show(that) {
alert(that.value);
}
</script>
</head>
<body>
<form method="post" action="$php_self">
<input type="checkbox" name="check" value="1" onclick="show(this)" />
<input type="checkbox" name="check" value="2" onclick="show(this)" />
<input type="checkbox" name="check" value="3" onclick="show(this)" />
</form>
</body>
</html>

You currently have no way of submitting this form...
Jul 23 '05 #2
Dnia Wed, 15 Sep 2004 21:29:28 GMT, McKirahan napisa³(a):

Is this what you want?


no... this function alerts me value of clicked checkbox...
I don't want value... i want to know number of array index

<input type="checkbox" id="id_number[]" name="check[]" value="1"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="2"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="3"
onclick="show()"/>

for value=2 <--- index is check[1]

in this example, I need 1 as result

for value=3 <--- index is check[2]

here I need 2 as result

etc...
Jul 23 '05 #3
Piotr wrote:
how can I read (in alert for example) array index number of checked
checkbox?

I have:

<input type="checkbox" id="id_number[]" name="check[]" value="1"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="2"
onclick="show()"/>
<input type="checkbox" id="id_number[]" name="check[]" value="3"
onclick="show()"/>

function show()
{
alert(here: display clicked id or name number of index? (for value "3" i
should be 2))
}

sorry for my english :)

Piotr,

You should not have duplicate id's, they should be unique. Most browsers will tolerate it, but it is contrary to the HTML spec. Any attempt to access the elements by id e.g. document.getElementById('id_number[]'), will likely return the first element only and may well cause an error.

Each of the elements in a form has a unique index, but the index is sequential across all the elements - there is no separate index for checkboxes and buttons. So if you put another element in the form, the index of all subsequent elements will be incremented by 1.

You can create an array of checkboxes by giving them all the same name and using a method like that below, but I'm not sure that it's a good idea. Presumably users willl be selecting a checkbox next to some value - why not just report the value?

Probably the best way to do what you ask is to give them a name equal to the index you'd like them to have, such as checkbox_1, checkbox_2, etc. This way you define exactly what the index is rather than using the order in some arbitrary array.

Using square brackets may cause you grief too but it's not fatal.

Below is some code that creates an array of input elements and reports the ones that are checked using both 'nodeType' and 'name' to create arrays. It also does a getElementById to show what happens with non-unique id's.

For the index method, the button is included in the array. If you put it at the top, the checkbox indices are all incremented by 1. For the name method, it isn't.

Cheers, Rob.

<form name="aForm">
<input type="checkbox" id="id_number[]"
name="check[]" value="1"
onclick="show()"/>
<input type="checkbox" id="id_number[]"
name="check[]" value="2"
onclick="show()"/>
<input type="checkbox" id="id_number[]"
name="check[]" value="3"
onclick="show()"/>
<input type="button" value="Input index"
onclick="
var a=this.form.elements;
var msg = document.getElementById('id_number[]');
for (var i=0;i<a.length;++i) {
msg += '\nnodeName ' + i + ': ' + a[i].nodeName
+ ' nodeType ' + a[i].nodeType
+ ' is';
if (!a[i].checked) msg += ' not ';
msg += ' checked.';
}
alert(msg);
">
<input type="button" value="Name index" onclick="
var c = 0;
var a=this.form.elements;
var msg = document.getElementById('id_number[]');
for (var i=0;i<a.length;++i) {
if (a[i].name == 'check[]'){
++c;
msg += '\nCheckbox ' + c
+ ' is';
if (!a[i].checked) msg += ' not ';
msg += ' checked.';
}
}
alert(msg);
">
</form>
Jul 23 '05 #4
Dnia Wed, 15 Sep 2004 22:55:38 GMT, RobG napisa³(a):

Thanks a lot Rob!
It's was very helpfull for me. Now - it works! :)
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jerome | last post by:
Hi, I've got the following problem: I'm retrieving data from an SQL Server database. And I want my form to display a checked box in the state corresponding to the value saved in the DB. But...
4
by: Kevin | last post by:
Hi, I have a input with type checkbox. I want it to be automatically checked if the value from the corresponding field in the database is also checked. I tried this: <% set objdc =...
0
by: mehul | last post by:
CheckBox template always evaluate to False even if checked in a DataGrid hosted inside a TabStrip in ASP.NET Hi, I am trying to develop an ASP.NET application. I am using TabStrip (which is...
4
by: moondaddy | last post by:
There are different times when I will have a group of checkboxes and need to force only one to be checked at a time. I would also like to do this client side and not require a postback. These...
1
by: dx | last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as simple as setting oCheckBox.Checked = True.. yet for some reason it isn't. I have a user control (ascx) that that has a...
4
by: sicapitan | last post by:
I have this code snippet: updateProps snippet: if (mycheckbox.checked == '1') ? $('mycheckbox').checked = true : $('mycheckbox').checked = false; content = $('mydiv').innerHTML;
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
2
by: ahmurad | last post by:
Dear all, I am fresh php programmer. I've spent much time to solve a checkbox related null value submission problem in php platform. I want to submit 4 checkbox value. if I checked all the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.