Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get div text value?

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);
like image 421
Greg Avatar asked Nov 12 '13 14:11

Greg


People also ask

How do I get div text?

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.

Can div contain text?

Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances. Save this answer.

Can you have a value in a div?

div elements don't have a . value property that would get submitted to the backend; use an input element for that.

How do I access a tag within a div?

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.


2 Answers

document.getElementsByClassName('question')[0].innerText;

or

document.querySelector('.question').innerText;
like image 196
CD.. Avatar answered Nov 01 '22 17:11

CD..


You can do it like this

alert(document.getElementsByClassName("question")[0].children[0].innerHTML);
like image 4
Yuriy Galanter Avatar answered Nov 01 '22 15:11

Yuriy Galanter