By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

UL LI get text

P: n/a
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,

Oct 28 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Oct 28, 6:18*pm, SonnyWibawa...@gmail.com wrote:
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...nt.textContent

[snip]
Oct 29 '08 #2

P: n/a
On 2008-10-29 02:01, David Mark wrote:
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.
liElement.firstChild.data should be supported in all reasonably current
browsers.
- Conrad
Oct 29 '08 #3

P: n/a
On Oct 28, 11:08*pm, Conrad Lender <crlen...@yahoo.comwrote:
On 2008-10-29 02:01, David Mark wrote:
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.

liElement.firstChild.data should be supported in all reasonably current
browsers.
(Assuming the list item has a single text node for a child.)
Oct 29 '08 #4

P: n/a
On Oct 29, 11:01 am, David Mark <dmark.cins...@gmail.comwrote:
On Oct 28, 6:18 pm, SonnyWibawa...@gmail.com wrote:
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.
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[i];

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
}
--
Rob
Oct 29 '08 #5

P: n/a
On Oct 29, 1:43*am, RobG <rg...@iinet.net.auwrote:
On Oct 29, 11:01 am, David Mark <dmark.cins...@gmail.comwrote:
On Oct 28, 6:18 pm, SonnyWibawa...@gmail.com wrote:
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.

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
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]
Oct 29 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.