Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!