1

I've got a js function using jquery that works fine with 1.8.3.js, but when I update to 1.9.1.js it quits. I really don't know anything about jquery at this point (it's on the list of things to study, coming up soon), so I really can't tell from looking at it where the problem might come in. Here is the code:

<script type="text/javascript">
  var allCheckBoxSelector = '#<%=GridView1.ClientID%> 
         input[id*="chkAll"]:checkbox';
  var checkBoxSelector = '#<%=GridView1.ClientID%> 
         input[id*="chkSelected"]:checkbox';

  function ToggleCheckUncheckAllOptionAsNeeded()
  {
    var totalCheckboxes = $(checkBoxSelector),
           checkedCheckboxes = totalCheckboxes.filter(":checked"),
           noCheckboxesAreChecked = (checkedCheckboxes.length === 0),
           allCheckboxesAreChecked = (totalCheckboxes.length ===
           checkedCheckboxes.length);

     $(allCheckBoxSelector).prop('checked', allCheckboxesAreChecked);
    }

    $(document).ready(function ()
    {
      $(allCheckBoxSelector).live('click', function ()
      {
        $(checkBoxSelector).prop('checked', $(this).is(':checked'));

        ToggleCheckUncheckAllOptionAsNeeded();
      });

      $(checkBoxSelector).live('click', ToggleCheckUncheckAllOptionAsNeeded);

      ToggleCheckUncheckAllOptionAsNeeded();
  });
</script>

Any suggestions on what the problem is here? Again, when referencing 1.8.3.js, it works perfectly, but when using 1.9.1.js checking the "check all" box doesn't have any effect.

3 Answers 3

2

The .live() function was deprecated in jQuery 1.7, and then finally removed entirely in jQuery 1.9. Your code no longer works because you're trying to call a function that doesn't exist any more. In place of .live(), use the delegated event syntax for .on(). So change this:

$(allCheckBoxSelector).live('click', function () {
    $(checkBoxSelector).attr('checked', $(this).is(':checked'));
    ToggleCheckUncheckAllOptionAsNeeded();
});

$(checkBoxSelector).live('click', ToggleCheckUncheckAllOptionAsNeeded);

to this:

$(document).on('click', allCheckBoxSelector, function () {
    $(checkBoxSelector).prop('checked', $(this).is(':checked'));
    ToggleCheckUncheckAllOptionAsNeeded();
});

$(document).on('click', checkBoxSelector, ToggleCheckUncheckAllOptionAsNeeded);

For additional details about converting .live() to .on(), see the API doc for .live().

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

1 Comment

I had just found that elsewhere. When I was googling apparently I was using the wrong keywords and could not find it (same as when I was making this post.) After the post was complete I saw a related post that referenced it, located here: stackoverflow.com/questions/14483514/… Thanks for the quick reply, I'll mark as answer in 6 minutes when I can. :)
2

.live has been removed in 1.9

try replacing :

$(checkBoxSelector).live('click', ToggleCheckUncheckAllOptionAsNeeded);

with

$(document).on('click',checkBoxSelector, ToggleCheckUncheckAllOptionAsNeeded);

Comments

1

Use .prop instead of .attr for checkboxes.

$(checkBoxSelector).prop('checked', $(this).is(':checked'));

1 Comment

I made that change, but it had no effect.

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.