Geting Checkboxlist value in javascript

S

Satheesh Babu B

hai..
am having a checkboxlist..now how do i get the value of the checkbox
that is checked in checkboxlist using javascript?


Thanks in advance....


Regards,
Satheesh
 
G

Gomolyako Eduard

What does mean "checkboxlist"? HTML doesn't contain "checkboxlist"
element.

Please, make a part of your HTML code here.

Best, Ed.
 
R

RobG

Satheesh said:
hai..
am having a checkboxlist..now how do i get the value of the checkbox
that is checked in checkboxlist using javascript?

If you mean a radio set, then you loop through the elements and find out
which one is checked. If you mean which checkbox is checked, it's much
the same - search through the checkboxes looking for the ones that have
been checked.

Just remember that when you reference form controls by name, you may get
a collection (radio buttons should always be a collection but it's not
guaranteed) or a single element

Here's some play code:

<form action="">
<div>
<input type="radio" name="rs1" value="zero" checked>zero
<input type="radio" name="rs1" value="one">one
<input type="radio" name="rs1" value="two">two
<br>
<input type="checkbox" name="cb1" value="cb 1">cb 1
<input type="checkbox" name="cb2" value="cb 2">cb 2
<input type="checkbox" name="cb3" value="cb 3">cb 3
<br>
<input type="button" value="Show checked radio value" onclick="
alert(showRadioChecked(this.form.rs1));
"><br>
<input type="button" value="Show all checked values" onclick="
alert(showAllChecked(this.form));
"><br>
<input type="reset">
</div>
</form>

<script type="text/javascript">

function showRadioChecked(rSet)
{
var i = rSet.length;
while(i--){
if (rSet.checked) return rSet.value;
}
}

function showAllChecked(f)
{
var el, els = f.elements;
var x = '';
var i = els.length;
while(i--){
el = els;
if (el.checked){
x += '\n' + el.type + ': ' + el.value;
}
}
return x;
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top