Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a JQuery mobile checkbox is checked

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 ?

like image 868
Sana Joseph Avatar asked Jun 21 '12 13:06

Sana Joseph


People also ask

How check if checkbox is checked jQuery?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);

How do you check if the checkbox is checked or not?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do you know checkbox is checked or not in android?

So, the method to know if the check box is checked is : (CheckBox) yourCheckBox. isChecked() it returns true if the check box is checked.


2 Answers

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()

like image 102
Prasenjit Kumar Nag Avatar answered Oct 23 '22 05:10

Prasenjit Kumar Nag


I have this code. I think you should adjust to your case.

  $('#formfriends input[type=checkbox]').each(function() {
    if ($(this).attr('checked'))
     //$(this).attr('disabled' , true);
     // do what you want here
  });
like image 35
Pedro Casado Avatar answered Oct 23 '22 04:10

Pedro Casado