0

I'm trying to add a multiple checkbox in a table.

and this is the code.

HTML

<HTML>
<HEAD>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <TITLE>Multiple Checkbox Select/Deselect - DEMO</TITLE>
</HEAD>
<BODY>
    <H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
    <th><input type="checkbox" id="selectall"/></th>
    <th>Cell phone</th>
    <th>Rating</th>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td>
    <td>BlackBerry Bold 9650</td>
    <td>2/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="2"/></td>
    <td>Samsung Galaxy</td>
    <td>3.5/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="3"/></td>
    <td>Droid X</td>
    <td>4.5/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="4"/></td>
    <td>HTC Desire</td>
    <td>3/5</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="case" name="case" value="5"/></td>
    <td>Apple iPhone 4</td>
    <td>5/5</td>
</tr>
</table>

</BODY>
</HTML>

jQuery

<SCRIPT language="javascript">
$(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");
        }

    });
});
</SCRIPT>

OK this works fine, but the problem is, what i'm going to do is not a static table.

it request and response and appends the table dynamically.

and if the rows are added like this. it doesn't handle the event.

Any good idea?

0

3 Answers 3

3

You need to use event delegation

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

 // if all checkbox are selected, check the selectall checkbox
 // and viceversa
 $(document).on('click', ".case", function () {
     $("#selectall").prop("checked", $(".case:not(:checked)").length == 0);
 });

Demo: Fiddle

Also use .prop() to set the checked state

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

3 Comments

yes i checked. if it is a static table. this code works. But in my situation this doesnt work
then there is some other issue
Thanks for the fast response. I got the bug :)
0

Jquery support event delegation, just like this:

$(document.body).on('click', '.case', function(){

});

Comments

0

Use .on() function. Examples can founded in http://api.jquery.com/on/

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.