2

I used jquery select all checkboxes to try and get a check all checkboxes button. Using the code from this answer, My button disappears immediately as seen on this Fiddle

JS:

$(function(){

    $('#checkAll').toggle(
            function(){
                    $('#numberList .numberClass').prop('checked',true);
            },
            function(){
                    $('#numberList .numberClass').prop('checked',false);
            });
});

HTML:

 <div id='numberList'>
 <table>
<tr>
<th>
    <input type='button' id='checkAll' name='checkAll' value='Check All'>
    </td>
</tr>
<tr>
<td>
    <input class='numberClass' type='checkbox' id='number[" . $k . "]' name='number[" . $k . "]'>
</td>
</tr>
<tr>
<td>
    <input class='numberClass' type='checkbox' id='number[" . $k . "]' name='number[" . $k . "]'>
</td></tr>
 </table>
 </div>

what is so obvious that i am missing?

1
  • Try $('#checkAll').click( Commented Aug 15, 2014 at 13:54

4 Answers 4

4

The .toggle(function, function) version (i.e. the event handling aspect) was removed in jQuery 1.9, as stated in the documentation. As such, it's treating it as the other .toggle() method, which is used to toggle the visibility of a set of elements - since it's set to visible with your HTML and CSS, jQuery is toggling it to not being visible.

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

1 Comment

ahh, thats an old post i was using. thanks. will accept in 9 minutes.
0

Are you sure about the toggle() function? The description in the jQuery documentation clearly states:

Display or hide the matched elements.

Have a look at the updated Fiddle: http://jsfiddle.net/o0bpdr53/12/

1 Comment

I kind of want to downvote just for using 0 and 1 to track a boolean state in a language that supports boolean true and false values... Use an actual boolean and you can get rid of the if statement entirely: jsfiddle.net/o0bpdr53/14
0

As Anthony said, using .toggle in the way you are here was deprecated in jQuery 1.8 and removed in 1.9. .toggle now toggles element visibility, which is why you're seeing the button disappear.

Reading the source for the now-removed .toggle in jQuery 1.8 could give a clue to fixing this. One way, to avoid global variables, would be to use .data() to store the current state of the checkboxes:

$('#checkAll').click(
    function () {
        var currentlyChecked = $(this).data('checked') || false,
            shouldBecomeChecked = !currentlyChecked;

        $('#numberList .numberClass').prop('checked', shouldBecomeChecked);

        $(this).data('checked', shouldBecomeChecked);
    }
);

http://jsfiddle.net/o0bpdr53/15/

Comments

0

My solution was the following:

        $('#checkAll').change(

            function(){
                    if ($(this).prop('checked')){
                            var status = true;
                    }
                    else{
                            var status = false;
                    }
                            $('.numberClass').prop('checked',status);
            });

Thanks to all the commenters.

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.