Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
React: Detect Selected option on Custom switch button

I have created a textual switch button using HTML and CSS. I am using that in a React app and I want to detect the selected option whenever it changes. for e.g. if OR is selected, it should be stored in selectedOption state and if AND is selected, that should be stored.

I have made a code sandbox with the demo (https://codesandbox.io/s/stupefied-snyder-lo530)

Can anyone please help me with this?

Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

set the state on click, by adding this:

onClick={(e) =>{ 
     selectedOption==="OR" ? setSelectedOption("AND") : setSelectedOption("OR"),
    console.log(selectedOption);
    }}
about 4 years ago · Juan Pablo Isaza Report

0

As pointed out by @pilchard in comment, e.target.checked can be used to detect true/false and then based on that selectedOption can be set.

onClick={e => {
    const currentVal = (e.target as HTMLInputElement).checked;
    setSelectedOption(currentVal ? 'AND' : 'OR');
}}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!