Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  52] [ 4]  / answers: 1 / hits: 21618  / 12 Years ago, fri, june 1, 2012, 12:00:00

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]);
});

More From » jquery

 Answers
114

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!


[#85202] Thursday, May 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samara

Total Points: 326
Total Questions: 106
Total Answers: 103

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;