Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Determine Which Button element is currently selected using JavaScript only!

How would I detect when the FUND LATER or FUND NOW button is selected or changed? I Know I have to use the class name of "active" for the selected button but not sure what else I need to do?
<div class="toggle-fieldView">
        <button type="button" data-value="Fund Now" class="">FUND NOW</button>
        <button type="button" data-value="Fund Later" class="active">FUND LATER</button>
</div>

Open in new window

This is a single page app so this content is added dynamically.
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

Hi Leakim,
What if there are other elements with "data-set" on the page? I don't want them to trigger this.

Thanks!
you can add additional tests and not just : if(event.target.dataset)
so you can test if :
- the target is a button
- the dataset.value if one of what you're looking for "Fund Now" or "Fund Value"
- tghe innerHTML if one of you're looking for "FUND NOW" or "FUND LATER"
- the parentNode (classes, tag,...)
- and so on
Avatar of MJ

ASKER

Thank you Leakim!