0

I have the following code:

<select id="deloptions">
<option value="none">Select none</option>
<option value="all">Select all</option>
</select>

I'm trying to check all theck and uncheck all the checkboxes from a table which is placed after this select. The checkboxes are placed in the 1st collumn of the table. They all have "delme" class. I've tried everything but it doesn't seem to work... Here is my current code:

$('#deloptions').change(function(){ 
var value = $(this).val();
if (value == 'all') {
    $('input:checkbox').prop('checked', true);
    } else {
    $('input:checkbox').prop('checked', false);
    }
});

I've tried using their class too but no success... like:

$('.delme').prop('checked', false);

Trying reaching for the table didn't work either... so i'm kindda stucked.

EDITED:

<table>
<tr>
<th></th>
<th>Title</th>
<th>Content</th>
<th>Date Added</th>
<th>Actions</th>
</tr>

<tr id="37">
<td><input type="checkbox" class="delme" id="37"></td>
<td>Good news!</td>
<td>This is a message!</td>
<td>2013-07-22</td>
<td>Here is a button</td>
</tr>
</table>
3
  • 1
    Works fine here Commented Jul 22, 2013 at 11:01
  • Are there any errors in your javascript console? Commented Jul 22, 2013 at 11:04
  • Code is fine with table also jsfiddle.net/ZKW6U Commented Jul 22, 2013 at 11:07

5 Answers 5

1

Try:

$('#deloptions').change(function(){ 
 var value = $(this).val();
 if (value == 'all') {
   $('input').attr('checked', 'checked');
 } else {
   $('input:checkbox').prop('checked', 'no');
 }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

$('input[type="checkbox"]').prop("checked", false);

1 Comment

Got it. The problem was that i've imported an older version of jquery (1.3.0) :D
0

Try this

 $(function(){
    $("#deloptions").change(function(){
       if($("#deloptions  option:selected").val()=="all")
       {
              $('input[type="checkbox"]').prop("checked", false);
       }
       else
       {
              $('input[type="checkbox"]').prop("checked", true);
       }
    });
 })

1 Comment

Got it. The problem was that i've imported an older version of jquery (1.3.0) :D
0

You may have not added jQuery reference as your code is working fine

jsFiddle

Comments

0

try this code.

$(function(){

// add multiple select / deselect functionality
$("#selectall").click(function () {
      $('.case').attr('checked', this.checked);
});

// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){

    if($(".case").length == $(".case:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }

});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.