user2569524 user2569524 - 5 months ago 74
Javascript Question

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!

Answer

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

Comments