tested. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title> plainText() </title> <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/javascript" src="/js/my.js"></script> <script type="text/javascript"> $(function(){ $("div,h1").each(function(){ alert ($(this).plainText().join("")) }) }); </script> </head> <body> <div><p>here is</p> some text</div> <h1><a href="#id1">nice</a>Header 1</h1> </body> </html>
On 5/29/07, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote:
something like this (untested) returns an array of all the plain text nodes in a jQuery object. jQuery.fn.plainText = function() { var text=[]; this.each(function(){ var children =this.childNodes; for (var i = 0; i < children.length; i++){ var child = children[i]; if (child.nodeType == 3) text[text.length] = $.trim(child.nodeValue); } }) return text; }; On 5/29/07, Ⓙⓐⓚⓔ <[EMAIL PROTECTED]> wrote: > > Dave, I was shocked when you got the first answer. I don't think there > is any way to get just the text using jQuery core functions. > > If you get .html() you can use regexps to remove the complete nodes, it > seems messy. > > if you write a plugin (there may already be one) it can go thru the dom > nodes and return the concatenation of the simple text nodes. > > jQuery likes to shield us from the nastiness of the real dom objects and > incompatibilities of lesser browser. > > > > On 5/29/07, DaveG < [EMAIL PROTECTED]> wrote: > > > > > > > > I didn't find a way to do this yet, but I did discover a way to crash > > my browser: > > > > Warning: locks up FF2 > > Input: <div id="id1"><p>here is</p> some text</div> > > jQ: $("#id1<").text() > > > > I also discovered a way to get the text of the first element: > > Input: <div id="id1"><p>here is</p> some text</div> > > jQ: $("#id1>").text() > > Output: "here is" > > > > ~ ~ Dave > > > > On Tue, 29 May 2007 15:48:30 -0400, DaveG <[EMAIL PROTECTED]> > > wrote: > > > > > > > > > How do I find the first text-node of a given DOM object? > > > > > > Input: <div><p>here is</p> some text</div> > > > Output: " some text" > > > > > > Input: <h1><a href="#id1"></a>Header 1</h1> > > > Output: "Header 1" > > > > > > ~ ~ Dave > > > > > > > -- > Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ -- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ
-- Ⓙⓐⓚⓔ - יעקב ʝǡǩȩ ᎫᎪᏦᎬ