I have a group of radio buttons with the same name, but different values. I'd like to do something like:
$("#langs").on("change", "[name=locale]", myfunction);
This works, but when I click on a new radio button myfunction gets called twice: once for the "old" radio button that is automatically getting unchecked, and once for the new one I'm clicking on.
changing onchange to onclick is not a solution, because I use it with jquery-mobile and it wraps the inputs with label, so the label is getting clicked, not the input.
You can pass $(this)
as an argument to myfunction
and then inside myfunction
check if the radio button is checked
$("#langs").on("change", "[name=locale]", function() { myfunction($(this)); } );
function myfunction(elem) {
if(elem.is(':checked')) {
// code here
}
}
Does this work for you? Following the jQM Docs
HTML:
<div data-role="page">
<fieldset data-role="controlgroup">
<legend>Choose a language:</legend>
<input type="radio" name="langs" id="english-lang" value="en" />
<label for="english-lang">English</label>
<input type="radio" name="langs" id="korean-lang" value="ko" />
<label for="korean-lang">Korean</label>
</fieldset>
</div>
JS:
$("input[name=langs]:radio").bind( "change", function(event, ui) {
console.log('Lang: '+$(this).val());
// Call function here
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With