I've got the following html:
<div class="question">
<div class="text">
Blah Blah
</div>
</div>
I am trying to get the value "Blah Blah" using javascript.
So something like?
alert(document.getElementsByName("question")[0].value);
Use the textContent property to get the text of a div element, e.g. const result = element. textContent . The textContent property will return the text content of the div and its descendants. If the element is empty, an empty string is returned.
Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.
div elements don't have a . value property that would get submitted to the backend; use an input element for that.
The Div Object in HTML DOM is used to represent the HTML <div> element. This tag is used to specify the container for other HTML elements to style them with CSS or to perform certain tasks with JavaScript. Syntax: The <div> element can be accessed by using getElementById() method.
document.getElementsByClassName('question')[0].innerText;
or
document.querySelector('.question').innerText;
You can do it like this
alert(document.getElementsByClassName("question")[0].children[0].innerHTML);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With