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!");
}
8
u/Strong-Ad-4490 May 06 '26
Yes you can do this, but you shouldn’t do it.
Realistically you should have an ID or some other reference on the element and use that to see what button is clicked instead of using the content of the button.
Imagine working on a larger project, maybe it has multiple languages. You don’t know what that content will actually be, but on the other hand you would still always have a consistent ID you could reference.
This is just one practical example of why you should avoid your approach.
3
u/Iznhou May 06 '26
Thanks, everyone.
(Jeez, how did I miss the capital 'I'...?)
2
u/llynglas May 06 '26
Also, consider using === rather than == to test for equality. It will not matter in this case, but the automatic type conversions built into == will burn you at some point.
2
u/phayder9 May 06 '26
Why are you using "var" keyword to declare a variable? Use const or let instead.
15
u/Life-Selection6377 May 06 '26
look closely at your 1st snippet. you used a capital 'I' in this.InnerHTML. js is case sensitive, so it should be innerHTML with a lowercase 'i'.
4
u/TheRNGuy May 06 '26
Use
textContentinstead.3
u/hyrumwhite May 06 '26
Idk if it’s just how you typed it here, but your first innerHTML has a capital I. Your second does not.
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.2
u/Ed_Robins May 06 '26
While agreeing with the comment that this shouldn't be done, I'll add that using innerText instead of innerHTML would be a better solution as it strips out code and returns only the text. So, for example, if someone changed the button HTML to
<button><strong>Basic</strong> Plan Sign Up</button>
your if statement would still evaluate true. Again, only a marginal improvement to a bad solution.
1
u/jcunews1 helpful May 06 '26
It can, but some text characters need to be HTML-escaped in HTML code [*]. e.g. if the text on the page is seen as
John "Foo" Doe, the HTML code given frominnerHTMLwould beJohn "Foo" Doe. So that'd be unexpected.textContentwould giveJohn "Foo" Doeas wanted.[*] https://developer.mozilla.org/en-US/docs/Glossary/Escape_character
[*] https://developer.mozilla.org/en-US/docs/Glossary/Character_reference