question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change event on radio button going on->off

See original GitHub issue

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.

  1. Is this intended behaviour?
  2. If so, would you think it’s a good idea to add an additional event that triggers in both cases?
  3. 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
sstephensoncommented, Apr 11, 2018

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.)

2reactions
javancommented, Apr 11, 2018

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/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found