Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  119] [ 1]  / answers: 1 / hits: 30443  / 12 Years ago, thu, june 21, 2012, 12:00:00

I have a list of Checkboxes & I want to get the status of each one of them.
The List is here :



<div data-role=fieldcontain>
<fieldset data-role=controlgroup>
<input type=checkbox name=Sunday id=Sunday-1 class=custom />
<label for=Sunday-1>Sunday</label>
<input type=checkbox name=Monday id=Monday-1 class=custom />
<label for=Monday-1>Monday</label>
<input type=checkbox name=Tuesday id=Tuesday-1 class=custom />
<label for=Tuesday-1>Tuesday</label>
</fieldset>
</div>


I usually Check the checkbox's status using this quote:



var isChecked = $('#CheckboxID').is(':checked');



But in my case now, is there a method to get the status of Jquery Mobile checkboxes all at once ?


More From » jquery

 Answers
7

You can do



var status = $('input[type=checkbox]').filter('.custom').map(function(){
return $(this).is(':checked') ? 1 : 0;

});


Then the status array will have an entry for each checkbox in the same order, 0 for unchecked and 1 for checked. It's not much of useful as you have no other information of the checkbox in the array.



If you want to perform operation based on the status you can use .each() instead, like



$('input[type=checkbox]').filter('.custom').each(function(){
if($(this).is(':checked')){
// perform operation for checked
}
else{
// perform operation for unchecked
}

});


UPDATE



If you want to build an array of objects with the name and status of the checkboxes you can do that with



var status = $('input[type=checkbox]').filter('.custom').map(function(){
var name = $(this).attr('name');
if($(this).is(':checked'))
return { 'name':name, 'status' : 'Checked'};
else
return { 'name':name, 'status' : 'UnChecked'};

});

console.log(status);​


Demo: http://jsfiddle.net/joycse06/rL3Ze/



Read more on .each() and .map()


[#84753] Wednesday, June 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;