If I have 3 radio buttons, is there a way through jQuery of finding out the value of the one that was selected before the user clicks a new one?
<div id="radio-group">
<input type="radio" id="radio-1" name="radios" value="1" checked="true" />
<input type="radio" id="radio-2" name="radios" value="2" />
<input type="radio" id="radio-3" name="radios" value="3" />
</div>
In the example avove, if the user clicks on radio-3
, I need a way to get 1
, so that I can carry out some formattion to it. Thanks.
Build A Paint Program With TKinter and Python The radiobutton has only two values, either True or False. If we want to get the output to check which option the user has selected, then we can use the get() method. It returns the object that is defined as the variable.
To find the selected radio button, you follow these steps: Select all radio buttons by using a DOM method such as querySelectorAll() method. Get the checked property of the radio button. If the checked property is true , the radio button is checked; otherwise, it is unchecked.
You can use mouseup
and change
event. In mouse up you will get the value of radio
button that is selected before selecting other radion button and change event will give the new selection of radio
button.
Live Demo
$('input[name=radios]').mouseup(function(){
alert("Before change "+$('input[name=radios]:checked').val());
}).change(function(){
alert("After change "+$('input[name=radios]:checked').val());
});
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