How to uncheck a radio button?
The out-of-the-box unchecking feature is non-existent for a radio button, so we engineer a workaround by using a hidden radio:
Just call uncheckRadio()
function anytime you want to deselect any checked radio button in the group, and let the hidden comrade carry the "checked"
burden for the team.
jQuery epiphany and JavaScript enlightenment
To swerve off the checked path with jQuery, look $(this).prop('checked', false)
way.
Hint: Always prefer .prop()
over .attr()
when tampering the 'checked' state in jQuery because prop()
is the eyewitness to changes made after loading the DOM, whereas attr()
stubbornly holds onto initial DOM values.
Going old school with plain JavaScript, the checked
state can be manipulated directly:
A game of radios: unchecking all
To bring about mass destruction of the checked status for all radio buttons in a group, extend your jQuery selector powers:
Erasing text field memories
In the event you need to simultaneously wipe out text fields, jQuery got your cloak:
A precision strike with event handling
To achieve accuracy that rivals a Predator drone, assign mousedown
events:
Post-AJAX: The aftermath
Once the hurricane of AJAX has passed, it's time for the cleanup operation a.k.a resetting the form:
More Sorcery: jQuery plugin, state tracking and more
The creation myth: jQuery plugin
For those wizards who hex forms regularly, consider crafting a jQuery plugin:
State affairs: Tracking changes
With jQuery, you can remember the state of checked status. Store it using .data()
and recall whenever:
The grand unchecking plan
Include all radio buttons and labels in event listeners for unchecking:
Thinking conditionally with variable tracking
Use a variable to incorporate conditional logic within event handlers:
Was this article helpful?