473,406 Members | 2,698 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,406 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 26949
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
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.