3

Javascript:

if (GetCookie('prev_radio_value')!=null){   
     alert(GetCookie('prev_radio_value'));
     $(":radio[value="+GetCookie('prev_radio_value')+"]").attr('checked',true);
     $(":radio[value="+GetCookie('prev_radio_value')+"]").triggerHandler('click');

     $(':radio:checked').triggerHandler('click');

     $('input:radio[name=theme]:checked').click();

    }else{
        alert("clicking first");
     $("input:radio:first").attr("checked", true).trigger("click"); 
     }

HTML code:

<ul>
 <li><input type="radio"  name="theme" value="theme1"/>Theme1</li>
 <li><input type="radio"  name="theme" value="theme2"/>Theme2</li>
 </ul>

this code is inside div -> 'checkbox_div'

click function :

$("#checkbox_div input:radio").click(function() {
  alert("clicked") ;
 });

I have used the trigger click in 3 ways but none of them worked or triggered the click event.

example link : http://jsbin.com/ezesaw/1/edit is not triggering the click event upon selection of first radio button.

6
  • Change attr() to prop(), as the checked property should always be set with prop(), otherwise you'll have issues. Also, you should be using the change event on a radio button, not the click event, and use input[type="radio"] not input:radio. Commented Jul 31, 2013 at 18:40
  • Also, what you're looking for is $('#checkbox_div input[type="radio"]:checked').trigger('click'); Commented Jul 31, 2013 at 18:42
  • $("input[type='radio' value="+GetCookie('prev_radio_value')+"]").prop('checked',true); ...throws me an exception .... like this ..........Uncaught Error: Syntax error, unrecognized expression: [type='radio' value=theme1] Commented Jul 31, 2013 at 18:53
  • @adeneo its not working :( Commented Jul 31, 2013 at 19:04
  • What the heck is that supposed to be? You should read the jQuery documentation, you can't do it like that. Commented Jul 31, 2013 at 19:27

5 Answers 5

2

Just call "click()" without the function as an argument

$("#checkbox_div input:radio").click();
Sign up to request clarification or add additional context in comments.

1 Comment

This is not working ..... its not triggering the click event .... but how does it even know which radio button is clicked ... could you pls correct if am thinkin wrong ?
1

It works just fine:

http://jsbin.com/ivojoz/1/edit

The problem may be that you are not selecting the radio correctly

try

$('#checkbox_div input[type="radio"]').click(function() {
  alert("clicked") ;
 });

2 Comments

this function works fine .... but i want to trigger the click event on a checked radio button ...upon document load....
click event is not triggered upon selection of first radio ...dono y .... http://jsbin.com/ezesaw/1/edit
0
    $("#checkbox_div").on('click','input:radio', function() {

      alert("clicked");

    });

Give this code a shot. It worked for me.

Comments

0

In case u are still interested in this question:

The click event is implied when selecting/checking a radio button, all you need is to bind a procedure to it, this is just ONE way:

//js for radio button click
$('input[type="radio"]').on('click', function(){
    window.alert($(this).val());
})

<!--html-->
<input type="radio" value="first" name="same"/>
<input type="radio" value="second" name="same"/>
<input type="radio" value="third" name="same"/>

Comments

0

Whether done manually or programmatically, it seems that clicking an already checked radio button does not necessarily have any effect!

Assuming HTML of:

<input type="radio" name="rv_submit" value="delete" id="rb2" checked>

Trigger the required action via jQuery as follows:

$('#rb2').prop('checked', false);   // Turn off 'checked' ...
$('#rb2').click();                  // ... or click() has no effect

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.