Change event on radio button going on->off
See original GitHub issueHi there,
I’m implementing a classic radio-button-reveals-text-field interaction, as used in questionnaires to add more details when a user checks a specific radio button.
<label>
<input type="radio" name="tk" value="yes" data-action="checkreveal#reveal" />
Yes
</label>
<label>
<input type="radio" name="tk" value="no" />
No
</label>
<input type="text" data-target="checkreveal.revealable" placeholder="Enter more details…" />
However, it seems that change
is only triggered when the input in case is checked, not when it becomes unchecked as a result of another input in the radio group being checked.
- Is this intended behaviour?
- If so, would you think it’s a good idea to add an additional event that triggers in both cases?
- There seems to be no way to access the DOM element who triggers an event (e.g. to see if it’s checked or not) without also making it a target. Is that so?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Change event on radio button going on->off #146 - GitHub
To get the element that triggers the event, use this.element in your controller. This is clearly stated in the handbook. EDIT: I misunderstood ......
Read more >OnChange event handler for radio button (INPUT type="radio ...
Use the onclick handler instead of onchange - you're changing the "checked state" of the radio input, not the value , so there's...
Read more >Problem switching a radio button on/off - Solar2D Forums
When you use an onPress event, the value of event.isOn is the value “before” the switch. This is important because when you use...
Read more >Guidelines for radio buttons - Windows apps | Microsoft Learn
A radio button can be cleared if a user selects another radio button in the same group, but it can't be cleared if...
Read more >Why is it impossible to deselect HTML "radio" inputs?
If you need on/off functionality, use checkboxes, but changing the functionality of an existing type of control is going to cause you huge...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just wanted to clarify a couple of points, for posterity:
In a controller,
this.element
always refers to the controller element (the element with thedata-controller
attribute).The first argument to an action method is the DOM event object. In an action method, you can use
event.target
to access the element which triggered the event, andevent.currentTarget
to access the element with thedata-action
attribute. (Sometimes those are the same element, sometimes not.)That’s correct. There is no native
change
event when deselecting a radio input. You could put yourcheckreveal#reveal
action on both inputs. Or, maybe just use a checkbox if you only need on/off states.In the future, please post questions like these in our community forum: https://discourse.stimulusjs.org/