[ACCEPTED]-click or change event on radio using jquery-radio-button
This code worked for me:
$(function(){
$('input:radio').change(function(){
alert('changed');
});
});
0
You can specify the name attribute as below:
$( 'input[name="testGroup"]:radio' ).change(
0
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.
Try
$(document).ready(
instead of
$('document').ready(
or you can use a shorthand 1 form
$(function(){
});
$( '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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.