Sorry, I must have misunderstood the context. I was responding to this:

Don't use .text(), you'll get an array-like object of text nodes


It seemed like you were saying that .text() returns an array-like object. But if it's used as a getter (without an argument), it returns a string. If there is more than one element in the jQuery object, it concatenates the text values. So, given the OP's markup, doing $('div li').text() would return "SymphilisGhonoreahAids". Anyway, I know you already know this. Just trying to clarify for others who are newer. Hope I didn't muddy the water even more.

@nick, Another way to get "Free sex" out of that div is to do this:

$('div')[0].firstChild.nodeValue

That will give you leading and trailing white space, though, so you can use jQuery's trim function to strip it:

$.trim( $('div')[0].firstChild.nodeValue );

You'll have to adjust the selector, too, if that div isn't the first one in your document.

--Karl

____________
Karl Swedberg
www.englishrules.com
www.learningjquery.com




On May 4, 2009, at 9:29 PM, mkmanning wrote:


Sorry, maybe my response was somewhat confusing, but I don't believe
you'll get a concatenated string (as the OP's followup indicates).

After using .filter(), you'll get an array-like object (it's still the
jQuery object) which contains the text nodes.
Calling .text() on those won't actually concatenate them though, as
they're text nodes.

Likewise, if you do $('div').contents().text() you'll only get the
text of the UL element, rather than a concatenation of that with the
previous text node.

@nick, you can get the text from the object with text[0].nodeValue.

HTH :)

On May 3, 4:08 pm, Karl Swedberg <k...@englishrules.com> wrote:
On May 2, 2009, at 9:28 PM, mkmanning wrote:

Don't use .text(), you'll get an array-like object of text nodes.

Tiny clarification: you'll get a concatenated string of text nodes.

--Karl

____________
Karl Swedbergwww.englishrules.comwww.learningjquery.com

On May 2, 2009, at 9:28 PM, mkmanning wrote:



Don't use .text(), you'll get an array-like object of text nodes. Try

var text = $('div').contents().filter(function(){return
this.nodeType==3;});
console.log(text)

On May 2, 6:06 pm, nick <nboutel...@gmail.com> wrote:
Thanks for the response. Are you sure thats correct though?

alert($('div').contents().filter(function(){return
this.nodeType==3;}).text());

returns empty.

Reply via email to