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

137
Visualizações
Why is the value of the radio button selected not updating?

Whenever I select a new radio button on my HTML, the function newChord() only outputs 1 to the console, no matter the option. For example, if I've selected the button Advanced instead of Beginner, the console should show 3 (associated to Advanced through JS), but it's still only showing 1 (associated to Beginner). The leads me to believe that there's something wrong with the if statements (vague I know...) but I can't figure out why it's working for the beginner option, but not any of the others.

HTML Code

<div class="difficulty_btn"><br>
          <form>
            <input type="radio" id="beginner_btn" name="difficulty" value="beginner" checked>
            <label for="beginner_btn">Beginner</label>

            <input type="radio" id="intermediate_btn" name="difficulty" value="intermediate">
            <label for="intermediate_btn">Intermediate</label>

            <input type="radio" id="advanced_btn" name="difficulty" value="advanced">
            <label for="advanced_btn">Advanced</label>

            <input type="radio" id="mixed_btn" name="difficulty" value="all">
            <label for="mixed_btn">Mixed</label>

          </form><br>
</div>

Javascript Code

function newChord() {
    
    if ($("input[name='difficulty']:checked").val('beginner')) {
        console.log(1);
        
    } else if ($("input[name='difficulty']:checked").val('intermediate')) {
        console.log(2);

    } else if ($("input[name='difficulty']:checked").val('advanced')) {
        console.log(3);
        
    } else if ($("input[name='difficulty']:checked").val('all')) {
        console.log(4);
    }
}

Any help is appreciated, thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This line

if ($("input[name='difficulty']:checked").val('beginner'))

is not comparing the checked attribute's value, it's assigning it.

You probably want something like this instead:

if ($("input[name='difficulty']:checked").val() == 'beginner') {
    console.log(1);
} else if ($("input[name='difficulty']:checked").val() == 'intermediate') {
    console.log(2);
}
.... and so on
about 4 years ago · Juan Pablo Isaza Relatório

0

Chris Smith is right, however you could improve it by storing the selected value in a variable. Also you don't need to use else if, using if is enough. I made a snippet with your code. I added the change() event trigger to output values in console.

$('input:radio[name="difficulty"]').change(function(){
    let checkedValue = $("input[name='difficulty']:checked").val();
    
    if (checkedValue =='beginner'){
        console.log(1);
    } 
    if (checkedValue == 'intermediate'){
        console.log(2);
    } 
    if (checkedValue == 'advanced'){
        console.log(3);
    }
    if (checkedValue == 'all'){
        console.log(4);
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="difficulty_btn"><br>
          <form>
            <input type="radio" id="beginner_btn" name="difficulty" value="beginner" checked>
            <label for="beginner_btn">Beginner</label>

            <input type="radio" id="intermediate_btn" name="difficulty" value="intermediate">
            <label for="intermediate_btn">Intermediate</label>

            <input type="radio" id="advanced_btn" name="difficulty" value="advanced">
            <label for="advanced_btn">Advanced</label>

            <input type="radio" id="mixed_btn" name="difficulty" value="all">
            <label for="mixed_btn">Mixed</label>

          </form><br>
</div>

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