0

I want to select all checkbox when i click the top check, below is my code:

run.html

<script language="JavaScript" src="media/js/jquery-1.4.2.min.js"></script>
<script language="javascript">
$("#selectAll").change(function() {
  $(".xxx:checkbox").attr('checked', this.checked);
});
</script>
Select All: <input type="checkbox" id="selectAll"><br />
<br />

One: <input type="checkbox" class="xxx" /><br />
Two: <input type="checkbox" class="xxx" /><br />
Three: <input type="checkbox" class="xxx" />

but why it not work?

4 Answers 4

3

Because selectAll does not exist at the time the <script> is run. So $("#selectAll") matches no elements. (jQuery doesn't warn you when you apply an operation to no elements, it just silently does nothing.)

Put the <script> below the <input>, or put the binding in a $(document).ready(function() { ... }); block to make the code run at page load time.

Aside: I would avoid use of the non-standard jQuery selectors like :checkbox wherever possible, as they force the use of the JavaScript Sizzle selector library instead of fast native querySelectorAll. input.xxx[type=checkbox] would be another way of saying it.

Sign up to request clarification or add additional context in comments.

1 Comment

it is not work@@ <script language="JavaScript" src="media/js/jquery-1.4.2.min.js"></script> <script language="javascript"> $(document).ready(function() { $("#selectAll").change(function() { $(".xxx:checkbox").attr('checked', this.checked); }); }); </script> Select All: <input type="checkbox" id="selectAll"><br /> <br /> One: <input type="checkbox" class="xxx" /><br /> Two: <input type="checkbox" class="xxx" /><br /> Three: <input type="checkbox" class="xxx" />
0

Below is the latest version, it can work

<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.js"></script> 

<script type='text/javascript'> 
$(document).ready(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});
</script> 

Comments

0

Alternative to $(document).ready(function(){ .... you can use the jQuery-Function "LIVE":

$("#selectAll").live('change',function() {
  $(".xxx:checkbox").attr('checked', this.checked);
});

You can test this on: http://jsfiddle.net/GLTQQ/

btw, the following:

$(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});

is the shortcut for:

$(document).ready(function(){
    $("#selectAll").change(function() {
        $(".checkbox_delete:checkbox").attr('checked', this.checked);
    });
});

Comments

0

Here is a simple example for select all Checkbox.....

<ul>
<li><input type="checkbox" id="select_all"/> Selecct All</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 1</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 2</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 3</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 4</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 5</li>
<li><input class="checkbox" type="checkbox" name="check[]"> This is Item 6</li>

<script>
  var select_all = document.getElementById("select_all"); //select all checkbox
   var checkboxes = document.getElementsByClassName("checkbox"); 
   //checkbox items

  //select all checkboxes
  select_all.addEventListener("change", function(e){
  for (i = 0; i < checkboxes.length; i++) { 
    checkboxes[i].checked = select_all.checked;
 }
});


for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].addEventListener('change', function(e){ //".checkbox" change 
    //uncheck "select all", if one of the listed checkbox item is unchecked
    if(this.checked == false){
        select_all.checked = false;
    }
    //check "select all" if all checkbox items are checked
    if(document.querySelectorAll('.checkbox:checked').length == checkboxes.length){
        select_all.checked = true;
    }
});
 }
 </script>

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.