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

198
Vistas
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 Respuestas
Responde la pregunta

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 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