I can't get the checked values of the dynamic check box. What am I doing wrong? /
Markup:
<html>
<body>
one<input type="checkbox" name='checkbox0' value="one_name" checked>
two<input type="checkbox" name='checkbox1' value="one_name1">
three<input type="checkbox" name='checkbox2' value="one_name2">
<input type="button" id="envoyer" value="Envoyer Reponse" />
</body>
</html>
Javascript:
$('#envoyer').click(function(e){
var myArray=new Array(4);
for ( var j = 0; j < 3; j++){
var check=$('input:checkbox[name=checkbox'+j+']').is(':checked');
if(check==true)
myArray[j]=$('input:checkbox[name=checkbox'+j+']').val();
}
alert('check '+" "+myArray[i]);
});
I can't get the checked values of the dynamic check box. What am I doing wrong? http://jsfiddle.net/hxfsB/17/
Markup:
<html>
<body>
one<input type="checkbox" name='checkbox0' value="one_name" checked>
two<input type="checkbox" name='checkbox1' value="one_name1">
three<input type="checkbox" name='checkbox2' value="one_name2">
<input type="button" id="envoyer" value="Envoyer Reponse" />
</body>
</html>
Javascript:
$('#envoyer').click(function(e){
var myArray=new Array(4);
for ( var j = 0; j < 3; j++){
var check=$('input:checkbox[name=checkbox'+j+']').is(':checked');
if(check==true)
myArray[j]=$('input:checkbox[name=checkbox'+j+']').val();
}
alert('check '+" "+myArray[i]);
});
Share
Improve this question
edited Jun 1, 2012 at 20:35
Jashwant
29k16 gold badges75 silver badges108 bronze badges
asked Jun 1, 2012 at 20:20
MarwaInsatMarwaInsat
2931 gold badge4 silver badges17 bronze badges
1
- Look at your browser's JavaScript console in the developer tools. – David Thomas Commented Jun 1, 2012 at 20:25
6 Answers
Reset to default 5You have an error when outputting myArray
in alert (there is no i
variable defined).
However, your code can be better structured. Here is one solution:
$("#envoyer").click(function(e) {
var myArray = [];
$(":checkbox:checked").each(function() {
myArray.push(this.value);
});
alert("Checked: " + myArray.join(","));
});
DEMO: http://jsfiddle.net/hxfsB/25/
You had an Uncaught ReferenceError: i is not defined
; I've updated your fiddle here:
http://jsfiddle.net/hxfsB/24/
$('#envoyer').click(function(e){
var myArray = new Array(3);
for ( var j = 0; j < 3; j++) {
var check=$('input:checkbox[name=checkbox'+j+']').is(':checked');
if(check==true)
myArray[j]=$('input:checkbox[name=checkbox'+j+']').val();
// Alert Current Selection //
alert('check ' + " " + myArray[j] );
}
});
Keep in mind: undefined
means the checkbox is NOT selected.
I hope this helps!
At your title suggests, if you want to get checked check-boxes values, you can do this,
Javascript:
$('#envoyer').click(function(e){
$('input[type="checkbox"]:checked').each(function(){
alert(this.value);
})
})
Markup
<html>
<body>
one<input type="checkbox" name='checkbox0' value="one_name" checked>
two<input type="checkbox" name='checkbox1' value="one_name1">
three<input type="checkbox" name='checkbox2' value="one_name2">
<input type="button" id="envoyer" value="Envoyer Reponse" />
</body>
</html>
try this out : http://jsfiddle.net/hxfsB/27/
there is an issue with the selector
$('#envoyer').click(function(e){
var myArray=new Array(3);
$('input[type=checkbox]:checked').each(function(i) {
myArray[i] = $(this).val();
//alert($(this).val());
});
});
The i
in myArray[i]
doesn't exist anywhere. You need to either put the alert inside the for loop and use myArray[j]
or create a new for loop using i