0

I am having a problem with my jQuery code in that for some reason it doesn't distinguish between checked and unchecked checkboxes in my situation. It recognises them on the first account (i.e. if it is unchecked to begin with, it recognises that it is in fact unchecked). But if you then uncheck it, it still thinks that it is unchecked.

You can see here: http://jsfiddle.net/3BZp4/1/

When checked, it will copy the row to the second table, but when you uncheck that same row, it still copies to the second table despite the selector asking for only unchecked rows.

$('#one tbody tr td input.checkbox:not(:checked)').on('change', function (e)
{ });

3 Answers 3

2

It looks like that you have bound a function to all input boxes that are currently unchecked. That function will always be bound to those checkboxes regardless of their state thereafter. You should check the checked status as part of the bound function, much the same as thecodeparadox has done.

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

Comments

1

You can try something like this:

$(':checkbox').on('change', function() {
  if(this.checked) {
     // do if checked
  } else {
    // do if not checked
  }
});

Your complete code will:

$('#one tbody tr td input.checkbox').on('change', function(e) {
    if (this.checked) {
        var row = $(this).closest('tr').html();
        $('#two tbody').append('<tr>' + row + '</tr>');
    }
});

Check this DEMO

4 Comments

@danrhul check the demo, I think you want that
I did check it, but it still has the same problem. Checking a row in the first table, copies the row to the second table (desired). But when I uncheck that same row, it still copies the row over to the second table.
OK, in the demo, it doesn't work but after applying it to my actual work it does so thank you :D
It's work fine ,but why when using $(':checkbox:checked'); doesn't give the expected result ?
1

Working demo Is this what you are looking for http://jsfiddle.net/gvDGS/2/

API http://api.jquery.com/not-selector/

Please let me know if I missed anything, hope this helps!

code // BInds to all the unchecked but checks if checkbox is checked or not.

$('input[type=checkbox]:not(:checked)').on('change', function() {

   if( $(this).is(':checked') ){
        var row = $(this).closest('tr').html();
        $('#two tbody').append('<tr>' + row + '</tr>');
    }
});

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.