Please see this Fiddle
HTML:
<form>
<button type="button" class="form-clear">Clear</button>
<button type="button" class="form-set">Set</button>
<input type="text" value="Preset value" />
<input type="checkbox" checked/>
<input type="checkbox" />
</form>
jQuery:
$(".form-clear").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox, :radio').attr('checked', false);
});
$(".form-set").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('New preset value');
$(':checkbox, :radio').attr('checked', true);
});
I'm guessing there's a conflict between the two functions?
The most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox.
Use .prop() instead of the .attr().
$(".form-clear").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox, :radio').prop('checked', false);
});
$(".form-set").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('New preset value');
$(':checkbox, :radio').prop('checked', true);
});
You need to use .prop()
to set the checked property
$(':checkbox, :radio').prop('checked', true);
Demo
Attributes vs. Properties
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