Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select and trigger click event of a radio button in jquery

Upon document load, am trying to trigger the click event of the first radio button.... but the click event is not triggered.Also, tried 'change' instead of click ...but its the same result.

$(document).ready(function() {     //$("#checkbox_div input:radio").click(function() {      $("input:radio:first").prop("checked", true).trigger("click");      //});        $("#checkbox_div input:radio").click(function() {        alert("clicked");      });  }); 

Please follow the below link to the question

Example: http://jsbin.com/ezesaw/1/edit

Please help me out in getting this right. Thanks!

like image 991
user2569524 Avatar asked Jul 31 '13 20:07

user2569524


People also ask

Can we use OnClick on radio button?

Responding to Click Events When the user selects one of the radio buttons, the corresponding RadioButton object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout.


2 Answers

You are triggering the event before the event is even bound.

Just move the triggering of the event to after attaching the event.

$(document).ready(function() {   $("#checkbox_div input:radio").click(function() {      alert("clicked");     });    $("input:radio:first").prop("checked", true).trigger("click");  }); 

Check Fiddle

like image 136
Sushanth -- Avatar answered Oct 09 '22 19:10

Sushanth --


Switch the order of the code: You're calling the click event before it is attached.

$(document).ready(function() {       $("#checkbox_div input:radio").click(function() {             alert("clicked");        });        $("input:radio:first").prop("checked", true).trigger("click");  }); 
like image 30
Jack Avatar answered Oct 09 '22 20:10

Jack