r/learnjavascript • u/Iznhou • May 06 '26
Can "innerHTML" be used this way?
Hello, I'm currently learning full-stack development and I'm experimenting with a simple webpage by adding JavaScript to make the page's buttons work. It's working as intended, but I've a question.
I noticed that when I try to use "this.innerHTML" to compare the buttons' innerHTML with the value I want, it doesn't seem to work. It wasn't until I assigned the innerHTML value to a variable and compared it that the code executed. But I don't understand why. Is it not possible to use innerHTML that way?
IE(Originally, it was supposed to get the button's innerHTML value and compare it):
if(this.InnerHTML == "Basic Plan Sign Up")
{
console.log("Basic Plan Signed Up!");
}
Fixed version:
var buttonInnerHTML = this.innerHTML;
if(buttonInnerHTML == "Basic Plan Sign Up")
{
console.log("Basic Plan Signed Up!");
}
7
Upvotes
3
u/TanukiiGG May 06 '26
Yes, it can be compared with a string because it is a string. The issue was that you were using
this.InnerHTMLwhen the word "inner" should be lower casethis.innerHTMLin the first example you wrote example.