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

204
Visualizações
Why isn't an if condition working with .html() ? (JQuery)

I want to execute a jQuery condition based on the .html() method. When I manually assign "A" to slot1 variable, my condition works. However, when I assign the "A" to the slot1 variable from the #set1 element via the .html() method, the "A" is well assigned and shows in console.log as slot1 returns "A", but my condition doesn't work (#result stays stuck on "no").

Any idea how to get the condition working with the .html() method?

    // EXECUTING FUSION
$("#fusion").click(function(){
    slot1 = $("#set1").html();
    console.log(slot1);
    
    if (slot1 == "A") {
        $("#result").html("ok");
    }
    else {
        $("#result").html("no");
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The code below works. Yours works too but fails in some circumstances. I believe the issues you are having is due to white space issues and how you are getting your 'slot1' input. If you are getting .html() from say, a contenteditable div like I have shown in my code below, then you need to remove the white spaces like I have done below because simply doing .trim() won't work since the spaces are encoded as &nbsp . If this helps you, an upvote would be appreciated

                    $("#fusion").click(function(){
                        let slot1 = $("#set1").html();
                        console.log(slot1);
                        slot1 = slot1.replace(/ /g, '');
                        console.log(slot1);
                        
                        if (slot1.trim() === "A") {
                            $("#result").html("ok");
                            console.log("ok");
                        }
                        else {
                            $("#result").html("no");
                            console.log("no");
                        }
                    });  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                <div id="set1" contenteditable="true">This is where you type input.</div>
                <p id="result">Result</p>
                <button type="button" id="fusion" class="btn btn-primary buttonSignup" >CREATE ACCOUNT NOW<i class="iconRequired icon-chevron-right"></i></button>
            

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