aaronb
aaronb

Reputation: 162

JQuery $.change doesn't notice change made by .prop("checked", true)

Not sure if this I've made a logical error, but after reading JQuery's documentation, I couldn't find an answer.

I'd like to change the checked radio button using a keypress. However, separate code using .change on the input:radio only notices the change made by a mouse click. Also, you can see the selected input change in the DOM, it's just that JQuery doesn't see the change. Why would that be?

$('body').on('keypress', function(args) {
    console.log(event.which);
    if (args.keyCode == 49) {
        $("#line-seg").prop("checked", true);
    }
});

$('input:radio').change(function(){
  // ...
});

Thank you!

Upvotes: 1

Views: 55

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 241188

Since you are programatically setting the checked property, you need to manually trigger a change event:

$("#line-seg").prop("checked", true).trigger('change');

or:

$("#line-seg").prop("checked", true).change();

Upvotes: 3

Related Questions