Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
How to write a switch statement for radio buttons

I'm working on a movie selector project. I have five radio buttons, after a user has clicked on a radio button, I want to show the movies with that value. I'm thinking a switch statement works the best with this project. I can't seem to get it work, though.

This is one of the radio buttons:

<input type="radio" id="avenger-movies" name="select-movie" value="avenger-movies">
                <label for="avenger-movies">Avenger films</label>

How do I write the statement for these buttons? This is my function thus far:

const getRadioButtons = document.getElementsByName("select-movie");
getRadioButtons.forEach((radiobtn) => {
    
    radiobtn.addEventListener('change', (event) => {

        function handleOnChangeEvent() {
            // console.log(event.target.value)
            const inputValue = event.target.value;
            switch (inputValue) {
                // Case..
            }

        }
        handleOnChangeEvent()

    })
    
})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

<div id="allRadio">
    <input type="radio" id="avenger-movies" name="select-movie" value="avenger-movies">
    <label for="avenger-movies">Avenger films</label>
    <input type="radio" id="hello" name="select-movie" value="avenger-movies">
    <label for="hello">hello world</label>
</div>
<script>
    document.getElementById("allRadio").addEventListener("click",(e)=>{
        switch(e.target.getAttribute("id")){
            case 'avenger-movies':
                alert(1);
                break;
            case 'hello':
                alert(2);
                break;
        }
    })
</script>

You can use event delegation to handle it, I hope it helps you.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda