[ACCEPTED]-click or change event on radio using jquery-radio-button

Accepted answer
Score: 112

This code worked for me:

$(function(){

    $('input:radio').change(function(){
        alert('changed');   
    });          

});

http://jsfiddle.net/3q29L/

0

Score: 72

You can specify the name attribute as below:

$( 'input[name="testGroup"]:radio' ).change(

0

Score: 16

Works for me too, here is a better solution::

fiddle demo

<form id="myForm">
  <input type="radio" name="radioName" value="1" />one<br />
  <input type="radio" name="radioName" value="2" />two 
</form>

<script>
$('#myForm input[type=radio]').change(function() {       
    alert(this.value);
});
</script>

You 5 must make sure that you initialized jquery above 4 all other imports and javascript functions. Because 3 $ is a jquery function. Even

$(function(){
 <code>
}); 

will not check jquery initialised 2 or not. It will ensure that <code> will run only 1 after all the javascripts are initialized.

Score: 7

Try

$(document).ready(

instead of

$('document').ready(

or you can use a shorthand 1 form

$(function(){
});
Score: 1

$( 'input[name="testGroup"]:radio' ).on('change', function(e) {
     console.log(e.type);
     return false;
});

This syntax is a little more flexible to 11 handle events. Not only can you observe 10 "changes", but also other types 9 of events can be controlled here too by 8 using one single event handler. You can 7 do this by passing the list of events as 6 arguments to the first parameter. See jQuery On

Secondly, .change() is 5 a shortcut for .on( "change", handler 4 ). See here. I prefer using .on() rather than .change 3 because I have more control over the events.

Lastly, I'm 2 simply showing an alternative syntax to 1 attach the event to the element.

More Related questions