0

I have the following code:

function disabler(){
    var content = document.getElementById("show").textContent;
    var correct = "Roll Number Correct";
    var incorrect = "Roll Number Incorrect";
    alert(content == correct);
    /* if (){
        alert(content);
    } */ 
}

I have checked the value of the content variable ( which is as same as the correct variable)and used the typeof operator to be sure of its type. To my knowledge, the alert should yield true but it is not. What could be the possible solutions?

5
  • 2
    Are you sure the content is correct? There could be extra whitespace or something. Commented Dec 17, 2017 at 16:00
  • console.log(content); and post output Commented Dec 17, 2017 at 16:00
  • 1
    Maybe has somenting extra whitespace try using document.getElementById("show").textContent.trim() Commented Dec 17, 2017 at 16:01
  • 2
    Yeah, without seeing what's coming in from #show, there's no way of knowing. Code is technically fine, probably just a data issue Commented Dec 17, 2017 at 16:01
  • Post your HTML for a Minimal, Complete, and Verifiable example Commented Dec 17, 2017 at 16:06

1 Answer 1

2

I think you have some white space. Check this example:

var content = document.getElementById("show").textContent;

var contentWithTrim = document.getElementById("show").textContent.trim();


console.log(content);
console.log(contentWithTrim);

console.log("content.length:", content.length);
console.log("contentWithTrim.length:", contentWithTrim.length);

console.log(content == contentWithTrim);

console.log(content == "My Content");
console.log(contentWithTrim == "My Content");
<div id="show">
   My Content
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Also, it's useful trick is to check content.length and contentWithTrim.length, because it might be hard to spot the whitespace characters when logged otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.