Radio-button JS if(radioButton.checked)

How to select all the Radio-buttons in the same group(same name attribute), and when checked, they will add certain classes to other elements, when not, they will remove them?
13 Replies
Jochem
Jochem2y ago
you can use document.querySelectorAll('input[type="radio"][name="yournamehere"]'); to find all the radio buttons with name yournamehere Then you can loop over that using forEach and addEventListener('change', () => {...}); to run code whenever the radio button changes value
13eck
13eck2y ago
Here's a very basic (and verbose) example: https://codepen.io/c__beck/pen/zYaEVpX
Dovah
Dovah2y ago
@jochemm Let me try that out
Jochem
Jochem2y ago
Beck's example is probably much better tbh
Dovah
Dovah2y ago
yeah I'm beginner so the code itself is hard to read
Jochem
Jochem2y ago
especially as a beginner, I'd suggest first trying to understand the code in the code pen and asking if anything is unclear
Dovah
Dovah2y ago
@cvanilla13eck Can I just DM you my problem so I can explain what is troubling me? I have solution, but it is super hard-coded and not dynamic. Or DM @jochemm if beck is busy? But the code-pen he sent seems like solution I just need to decipher it
Jochem
Jochem2y ago
Why work in DMs when you can potentially get answers from both of us by explaining in here? with the added benefit that other people might still show up and help, or someone else might find this later and it helps them too?
Dovah
Dovah2y ago
Agree. Just did not want to clog it. xD Forgot it is thread in itself I think the code-pen he send is the answer. It is just too complicated. I need to first understand it.
13eck
13eck2y ago
If you want to hop in a video call I can walk you through it, @Dovah
Dovah
Dovah2y ago
That sounds nice! But currently my home is busy and hence noisy, so me talking would be a problem. But we can try! ?
13eck
13eck2y ago
Hope in the private chat room You'll see me there
Dovah
Dovah2y ago
sec
Want results from more Discord servers?
Add your server
More Posts