Setting a radio button with .prop() is not triggering event handler

user2755541

I have a JavaScript function that listens for a change on a Radio button in order to execute the next batch of code. However, I find that when I physically click the button, it's different from setting it in code (through .prop("checked",true);). Why when I change which radio button is selected through .prop() does it not trigger the event handler .change()? How can I adjust my code so that the handler hears the change?

Here's a JSFiddle that demonstrates the problem

Click the Radio buttons and the value will appear in a div below. However, when you click the "change to blue" button it will reset the Radio button to the "blue" option but the value will not be updated in the "output" div.

HTML:

<div id='colors'>
    <input type="radio" id="black" value="Black" name="myRadio" /><label for="black">Black</label><br>
    <input type="radio" id="blue" value="Blue" name="myRadio"/><label for="blue">Blue</label><br>
    <input type="radio" id="green" value="Green" name="myRadio"/><label for="green">Green</label>
</div>

JS:

$('#changeRadio').click(function(){
     $("input:radio[id='blue']").prop("checked",true);
});

$('#colors input').change(function(){
    var selected_item = $(this).val()
    $('#output').html(selected_item);
});
Netorica

You are just changing the state of the Checkbox and not trigger an event on the object; you need to do something that triggers event and changes the state at the same time.

Change:

$("input:radio[id='blue']").prop("checked",true);

To:

$("input:radio[id='blue']").trigger('click');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting a radio button with .prop() is not triggering event handler

From Dev

Button click event not triggering

From Dev

Button click event not triggering

From Dev

Setting the value of a radio button

From Dev

Onclick event triggering onload for button

From Dev

Timer event is not triggering button click

From Dev

Timer event is not triggering button click

From Dev

Triggering closest button on a delegated event

From Dev

Radio button click event

From Dev

Radio button event not firing

From Dev

jQuery: why doesn't radio button's change event handler retrigger?

From Dev

Setting value of radio button in Codeigniter

From Dev

Jquery - setting radio button with ajax

From Dev

MVVMCross Bound Button event not triggering on press

From Dev

Triggering OnCancel event of kendo upload on click of button

From Dev

Setting radio button checked true on button click

From Dev

Add the same event handler to radio buttons and checkboxes

From Dev

JavaScript button event handler not working

From Dev

Event handler for dynamically created Button

From Dev

Event not triggered on appended radio button

From Dev

Backbone radio button change event

From Dev

Backbone radio button change event

From Dev

Event not triggered on appended radio button

From Dev

If A Radio Button is Checked Call Function Handler

From Dev

Why does event handler wait until triggering code completes execution?

From Dev

Dynamically setting polymer custom element event handler

From Dev

NullPointerException when setting the Event Handler for Spinner

From Dev

Bootstrap with Radio Button toggle not triggering async postback inside update panel

From Dev

triggering button click event in table row with mouseup event

Related Related

HotTag

Archive