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

125
Views
How can I check the radio button with a single click? and how to select one by default?

I am trying to make a simple react app that displays radio input for each 'hero' from a list of heroes and if the user checks, the hero's name will be displayed as the favorite hero. But the problem is on my local machine to check a hero I need to double click that radio input. How can I check a hero with a single click? Code of the app.js file is given below:

import React, { useState } from "react";
import "./App.css";
function App() {
    const [heros, setHeros] = useState([
        "Superman",
        "Batman",
        "Antman",
        "Robocop",
    ]);
    const [selected, setSelected] = useState(null);

    const handleChange = (e) => {
        e.preventDefault();
        setSelected(e.target.value);
        console.log(e.target.checked);
    };


    return (
        <div>
            <h1>Select Your Favorite Hero</h1>
            <form onChange={handleChange}>
                {heros.map((hero, index) => (
                    <div key={index}>
                        <input
                            type="radio"
                            name="hero"
                            id={hero}
                            value={hero}
                            
                        />
                        <label htmlFor="{hero}">{hero}</label>
                        <br />
                    </div>
                ))}
            </form>
            <div>
                <p>Your super hero is: {selected}</p>
            </div>
        </div>
    );
}
export default App;
about 4 years ago · Juan Pablo Isaza
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!