2

I have a simple script that on <a> click it checks or unchecks a checkbox. All works fine the first time, but once the value is altered with jQuery it’s not being updated (though in Chrome’s inspector the attribute checked="checked" is being added/removed).

HTML:

<a href="#" class="js-tick-untick-checkbox">
    <input type="checkbox" class="send-invite-checkbox" />
    Something something in the month of May
</a>

jQuery:

$('.js-tick-untick-checkbox').click(function() {
  var checkbox = $(this).find('.send-invite-checkbox');

  if ($(checkbox).attr('checked') === 'checked') {
    checkbox.removeAttr('checked');
    $(this).removeClass('selected-element');
  } else {
    checkbox.attr('checked', 'checked');
    $(this).addClass('selected-element');
  }
  return false;
})

Check out the demo: http://jsfiddle.net/y3e0jfc6/

I tried various other options, like checked="true" / checked="false", none work…

Any ideas? Thanks.

5 Answers 5

2

Use prop() instead of attr()

From docs:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

$('.js-tick-untick-checkbox').click(function () {
    var checkbox = $(this).find('.send-invite-checkbox');
    if ($(checkbox).prop('checked')) {
        checkbox.prop('checked', false);
        $(this).removeClass('selected-element');
    } else {
        checkbox.prop('checked', true);
        $(this).addClass('selected-element');
    }
    return false;
})

Demo

Sidenote: you can do it like this as well.

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

3 Comments

Thanks, should’ve used prop().
@ TJ, I will suggest use checkbox.prop('checked', true); instead of checkbox.prop('checked', true);
@AmitAgrawal did you mean the quotes around true..? i prefer boolean also… updated...
1

You should be using $.prop() instead.

checkbox.prop('checked', true);
checkbox.prop('checked', false);

Comments

1

Try this code

$('.js-tick-untick-checkbox').click(function() {
  var checkbox = $(this).find('.send-invite-checkbox');

    if ($(checkbox).is(':checked')) {
    checkbox.prop('checked', false);
    $(this).removeClass('selected-element');
  } else {
    checkbox.prop('checked', true);
    $(this).addClass('selected-element');
  }
  return false;
});

Comments

1

You should use .prop instead of .attr

updated jquery

$('.js-tick-untick-checkbox').click(function() {
var checkbox = $(this).find('.send-invite-checkbox');

if ($(checkbox).prop('checked') == true) {
checkbox.prop('checked', false);
$(this).removeClass('selected-element');
} else {
checkbox.prop('checked', true);
$(this).addClass('selected-element');
}
return false;
})

Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. Source

1 Comment

this is also suggested by TN. Any difference
0
$('.js-tick-untick-checkbox').click(function() {
  var checkbox = $(this).find('.send-invite-checkbox'); 
    if ($(checkbox).is(':checked')) {
    $(checkbox).removeAttr('checked');
  } else {
    $(checkbox).prop('checked', 'checked');       
  }
   $(this).toggleClass('selected-element');
  return false;
})

Demo:

http://jsfiddle.net/4ywmLr3u/1/

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.