473,769 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26978
"Piotr" <pi**@gaztea.pl > wrote in message
news:5j******** *************** *****@40tude.ne t...
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.valu e);
}
</script>
</head>
<body>
<form method="post" action="$php_se lf">
<input type="checkbox" name="check" value="1" onclick="show(t his)" />
<input type="checkbox" name="check" value="2" onclick="show(t his)" />
<input type="checkbox" name="check" value="3" onclick="show(t his)" />
</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.getEle mentById('id_nu mber[]'), 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.ele ments;
var msg = document.getEle mentById('id_nu mber[]');
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.ele ments;
var msg = document.getEle mentById('id_nu mber[]');
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
4602
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 somehow that doesn't work!? SQL tells me the value is 1. My ASP page tells me it's 'True'??
4
9796
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 = Server.CreateObject("ADODB.Connection") etc ... sql="select maa13 from mytable" dim rsArray
0
1857
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 part of IE WebControls). Inside a tab I have a datagrid defined as follows:
4
10551
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 checkboxes could be just a group of check boxes across the top of a page, or could be part of a datalist or datagrid. How do I make it so when a use clicks on one checkbox that if another checkbox is checked, it becomes unchecked? Also, is it...
1
4511
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 checkbox and I can't get it to default to checked. I tried radiobuttons and experienced the same result.. can't start them as checked. The really frustrating thing is that I set the attributes of other input controls in the Init() with no problem. ...
4
4514
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
4101
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 with my problems. Now for my new little problem,I had a problem posting the values from checkbox fields to a database and thats the obstacle I overcame. Now the second part is my new problem is that I want that the next time that page loads for...
2
2390
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 checkbox, all the checked value is submitted properly. if checked any checkbox , the corresponding checkbox value is submitted properly. But I need to send the non checked value (please let non checked value is N ) with the checked checkbox value. ...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.