Hi 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.
To get the element that triggers the event, use EDIT: I misunderstood the question, see https://github.com/stimulusjs/stimulus/issues/146#issuecomment-380490637 for correct answer.this.element in your controller. This is clearly stated in the handbook.
Thanks @ngan. I must've missed the this.element. I find it hard to find stuff in the Handbook as it's written tutorial-style, and not as an API reference.
I'll wait for an answer from a core team member about the idea.
This is how the native DOM behaves.
That's correct. There is no native change event when deselecting a radio input. You could put your checkreveal#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/
Just wanted to clarify a couple of points, for posterity:
In a controller, this.element always refers to the controller element (the element with the data-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, and event.currentTarget to access the element with the data-action attribute. (Sometimes those are the same element, sometimes not.)
Most helpful comment
Just wanted to clarify a couple of points, for posterity:
In a controller,
this.elementalways refers to the controller element (the element with thedata-controllerattribute).The first argument to an action method is the DOM event object. In an action method, you can use
event.targetto access the element which triggered the event, andevent.currentTargetto access the element with thedata-actionattribute. (Sometimes those are the same element, sometimes not.)