Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
How get input value to add as class for current selected element in jquery

Hey guys i'm trying to make jquery code to change the icon's class by selecting radio input that has the value of class but the problem is its add the class vale to all others icons class's

here my code: HTML

<div class="select-container">
    <input type="radio" value="icon-home">
    <input type="radio" value="icon-car">
    <input type="radio" value="icon-sky">
    <input type="radio" value="icon-color">
</div>


<div class="container-icons">
    <a>
        Home Icon: <i class="icon"></i> <button class="select"></button>
    </a>
    <a>
        Car Icon: <i class="icon"></i>  <button class="select"></button>
    </a>
    <a>
        Sky Icon: <i class="icon"></i>  <button class="select"></button> 
    </a>
    <a>
        color Icon: <i class="icon"></i> <button class="select"></button>
    </a>
</div>

Jquery:

<script>
    jQuery('body').on('click' , '.select' , function() {

        jQuery('.select-container').show();

        $('.select-container').on('change', function () {

            var icon_value = $("input[type='radio'][name='icon']:checked").val();

            $('.icon').addClass(icon_value);

        });

    });

</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

On click, use .index to get the index of the selected radio among its siblings. Then with .eq, you can select the same index of the .icons and add the appropriate class.

$('.select-container input').on('change', function() {
  const value = $(this).val();
  const index = $(this).index();
  console.log(index);
  $('.icon').eq(index).addClass(value);
});
.icon-home {
  background: green;
}
.icon-car {
  background: yellow;
}
.icon-sky {
  background: blue;
}
.icon-color {
  background: orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="select-container">
  <input type="radio" value="icon-home">
  <input type="radio" value="icon-car">
  <input type="radio" value="icon-sky">
  <input type="radio" value="icon-color">
</div>


<div class="container-icons">
  <a>
        Home Icon: <i class="icon">A</i> <button class="select"></button>
    </a>
  <a>
        Home Car: <i class="icon">A</i>  <button class="select"></button>
    </a>
  <a>
        Home Sky: <i class="icon">A</i>  <button class="select"></button> 
    </a>
  <a>
        Home color: <i class="icon">A</i> <button class="select"></button>
    </a>
</div>

If you wanted to make only one radio button selected at a time, add the same name attribute to all the radio buttons.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda