UL LI get text

S

SonnyWibawaAdi

Hi All,

I want a simple javascript to just get the text in HTML list. Example
below, I want to get 'Here is' text. I tried innerHTML with a list of
'Here is My item 1 My item 2'. That simple.

<UL>
<LI>Here is</LI>
<UL>
<LI>My item 1</LI>
<LI>My item 2</LI>
</UL>
</LI>
</UL>


Thanks,
 
D

David Mark

Hi All,

I want a simple javascript to just get the text in HTML list. Example
below, I want to get 'Here is' text. I tried innerHTML with a list of

So you want the text from a list item? Certainly the innerHTML
property would not be the best choice. If this is to be a cross-
browser solution, you will need a wrapper that returns the innerText
or textContent property of the element. In most browsers, one or the
other (or both) will be set.

Start here:

https://developer.mozilla.org/En/DOM/Element.textContent

[snip]
 
R

RobG

So you want the text from a list item? Certainly the innerHTML
property would not be the best choice.

I'll second that.

If this is to be a cross-
browser solution, you will need a wrapper that returns the innerText
or textContent property of the element.

That has issues too. Safari's innerText property doesn't return the
text of elements hidden or made not visible by CSS and Opera will
return the content of script elements (perhaps not issues in this
case, but worth noting for a general case). The only really reliable,
cross-browser method is to recurs down the DOM tree and extract the
text from text nodes.

There is also the issue of white space, which is treated differently
in various browsers, so the getText function needs to consider whether
to normalise that (which usually means removing as much as possible, a
"lowest common denominator" approach) or letting the calling function
sort it out.

In most browsers, one or the
other (or both) will be set.

A simple version is:

function getText(el) {
if (typeof el.textContent == 'string') return el.textContent;
if (typeof el.innerText == 'string') return el.innerText;
}

A recursive method where script stripping is desired:

function getText(el)
{
var x = el.childNodes;
var txt = '';
var node;

for (var i=0, len=x.length; i<len; ++i){
node = x;

if (3 == node.nodeType) {
txt += node.data;
} else if (1 == node.nodeType){

if (node.tagName && node.tagName.toLowerCase() != 'script') {
txt += arguments.callee(node);
}
}
}
return txt.replace(/\s+/g,' '); // Maybe trim leading
// and trailing whitespace too
}
 
D

David Mark

I'll second that.


That has issues too.  Safari's innerText property doesn't return the

I wouldn't test that one first.
text of elements hidden or made not visible by CSS and Opera will
return the content of script elements (perhaps not issues in this
case, but worth noting for a general case).  The only really reliable,
cross-browser method is to recurs down the DOM tree and extract the
text from text nodes.

That is true.
There is also the issue of white space, which is treated differently
in various browsers, so the getText function needs to consider whether
to normalise that (which usually means removing as much as possible, a
"lowest common denominator" approach) or letting the calling function
sort it out.

I typically let the calling function sort out.

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top