How Can I Extract Text from HTML Tags in JavaScript?

0
15
Asked By CuriousCoder42 On

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);
}

```

5 Answers

Answered By ExpertDev123 On

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!

Answered By CuriousCoder42 On

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.

Answered By MDNfan33 On

MDN is life for learning web development!

Answered By JSNinja99 On

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!

Answered By WebWizard01 On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.