Alon Shmiel
Alon Shmiel

Reputation: 7121

Get text of div

I want to get the text of the next id (the text is: "Show More").

<div id="show_more_less" onclick="Show_More_Less();">Show more</p>

function Show_More_Less() {
    var str = document.getElementById('show_more_less').text;
}

I tried: .value but it doesn't work.

Upvotes: 0

Views: 93

Answers (3)

urraka
urraka

Reputation: 1017

Should make some checks to see if childNodes[0] exists and if it's a text node, but basically:

var str = document.getElementById('show_more_less').childNodes[0].nodeValue;

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

To get the text of an element in a cross browser way, you can do this :

var e = document.getElementById('show_more_less');
var text = e.textContent || e.innerText;

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

Try innerHTML:

var str = document.getElementById('show_more_less').innerHTML;

Also you have an opening <div> tag and a closing </p> tag which is inconsistent. You probably meant:

<div id="show_more_less" onclick="Show_More_Less();">Show more</div>

Upvotes: 1

Related Questions