How Can I Extract Text from HTML Tags in JavaScript?

November 8, 2025 5 answers
Asked by CuriousCoder42
I'm learning JavaScript and want to pull a value from between HTML tags. In my code, I'm checking if the text matches 'Ready' and changing the background color to green if it does; otherwise, it should turn red and alert me of the checked value. However, the expected result isn't being produced. Here's a snippet of what I'm working with: ```html
Ready
if (document.getElementById('statusBox').nodeValue == 'Ready') { document.getElementById('statusBox').style.backgroundColor = 'green'; } else { document.getElementById('statusBox').style.backgroundColor = 'red'; alert(document.getElementById('statusBox').nodeValue); } ```

Answers

WebWizard01
You should definitely check out structured learning resources like the MDN guides. They're super helpful for understanding how to work with HTML and JavaScript!
Reply to this answer
JSNinja99
Have you tried using `.innerHTML`? That's the way to go if you're trying to get text between tags!

CuriousCoder42: That did it. Thanks!

Reply to this answer
MDNfan33
MDN is life for learning web development!
Reply to this answer
CuriousCoder42
Much appreciated! I was just googling around and experimenting with different things in my IDE until I figured it out. That's how I got here, lol.
Reply to this answer
ExpertDev123
Itโ€™s surprising you went with `nodeValue`. I rarely use it for the DOM. You might want to use either `innerText` or `textContent` instead. They are easier to work with for your case!
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions