Reputation: 1972
I am wondering if there is a way to reset a radio to it's originally selected option. I know of defaultValue
for inputs but is there a way to make it so that I can reset the radios back to their originally selected value on page load?
I am not wanting to simply unselect all radios. I am trying to put them back to their originally selected value.
Thanks for any info.
Upvotes: 2
Views: 133
Reputation: 1000
I will rather make a jQuery plugin like this:
$.fn.defaultVal = function (checked) {
if(checked === true || checked === false) {
$(this).attr("data-default", checked);
return;
}
if(!$(this).attr("data-default")) {
console.log("No default value assigned!");
return;
}
return $(this).attr("data-default");
}
JSFIDDLE LINK UPDATE Working Demo is here: JSFIDDLE
Upvotes: 0
Reputation: 144709
Yes, radio inputs do have defaultValue
property, but what you are looking for is the defaultChecked
property:
$('input[type=radio]').prop('checked', function() {
return this.defaultChecked;
});
That being said, if you want to reset the form you can use the reset
method of the HTMLFormElement
object:
$('#formElement').get(0).reset();
Upvotes: 2
Reputation: 25892
I think you want this
<form action="">
<input type="radio" name="gender" value="male" checked="true"/>Male
<input type="radio" name="gender" value="female"/>Female
<input type="reset" value="reset"/>
</form>
Any time you will reset the form, the male
radio button will be selected.
Upvotes: 0