Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Triggering a click event on a radio button on page load

Tags:

jquery

I have 3 radio buttons which hide/show some div's. The radio buttons are getting generated by a php script, I want jquery to check the first radio button (works already!) and then trigger the click function so it hides/shows the related stuff.

$("input:radio:first").attr("checked", true).trigger("click");

The .trigger("click"); actually does not work.

The click event of the radio buttons looks like this:

$("input[name='type']").on("click", function() { ... });

Any ideas?

like image 342
peipst9lker Avatar asked Mar 29 '12 09:03

peipst9lker


People also ask

How can add click event in radio button?

To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How can add trigger click event in jQuery?

If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works: $(document). on("click", "a", function(){ $(this). text("It works!"); }); $(document).

What is trigger (' click ')?

Use Google Tag Manager's click trigger to fire tags based on click events. When an element is clicked on a page that matches the trigger conditions, Tag Manager will automatically populate values for any active click-based built-in variables.


1 Answers

Try doing it after DOM ready, like this:

$(function(){
  $("input:radio:first").click();
});

Maybe it hasn't finish loading. use .click.

like image 102
bArmageddon Avatar answered Sep 24 '22 08:09

bArmageddon