[jQuery] Re: select text for LI

2007-11-03 Thread Richard D. Worth
I've solved this same problem in the past with the wrapText plugin, by
George Adamson:

// Plugin to wrap html around all non-empty text nodes within an element:
(ignores text in child elements)
// By George Adamson, SoftwareUnity.com, March 2007.
$.fn.wrapText = function(html){
return this.each(function(){
$(this.childNodes).filter("[EMAIL PROTECTED]").each(function(){
if($.trim(this.nodeValue).length > 0)
$(this).wrap(html);
})
});
};

- Richard

On Nov 2, 2007 7:48 PM, Wizzud <[EMAIL PROTECTED]> wrote:

>
> _IF_ you only have text in your LIs then...
>
> $('li').each(function(){
>  alert($(this).html().match(/^([^<]*)/)[0]);
> });
>
>
> On Nov 2, 5:35 pm, "Jonathan Sharp" <[EMAIL PROTECTED]> wrote:
> > I'm sure there's a more elegant solution but you could do something
> like:
> >
> > // Untested
> > var about = $( 'selector for the li' ).clone().find('>
> > ul').remove().end().html();
> >
> > Cheers,
> > -Jonathan
> >
> > On 11/2/07, sawmac <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > > I'm trying to select text inside list items. I'm using
> > > jQuery's .text( ) method. Unfortunately, that returns the text of all
> > > children as well. That means for a nested list like this
> >
> > > 
> > > index.html
> > > about
> > >
> > >  index.html
> > >  more.html
> > >
> > > 
> > > 
> >
> > > $('li').eq(1).text() returns
> > > 'about
> > >   index.html
> > >   more.html'
> >
> > > I just want to retrieve "about" not the text from the child list
> > > items. Any ideas on how to do that?
> >
> > > thanks
>
>


[jQuery] Re: select text for LI

2007-11-02 Thread Wizzud

_IF_ you only have text in your LIs then...

$('li').each(function(){
  alert($(this).html().match(/^([^<]*)/)[0]);
});


On Nov 2, 5:35 pm, "Jonathan Sharp" <[EMAIL PROTECTED]> wrote:
> I'm sure there's a more elegant solution but you could do something like:
>
> // Untested
> var about = $( 'selector for the li' ).clone().find('>
> ul').remove().end().html();
>
> Cheers,
> -Jonathan
>
> On 11/2/07, sawmac <[EMAIL PROTECTED]> wrote:
>
>
>
> > I'm trying to select text inside list items. I'm using
> > jQuery's .text( ) method. Unfortunately, that returns the text of all
> > children as well. That means for a nested list like this
>
> > 
> > index.html
> > about
> >
> >  index.html
> >  more.html
> >
> > 
> > 
>
> > $('li').eq(1).text() returns
> > 'about
> >   index.html
> >   more.html'
>
> > I just want to retrieve "about" not the text from the child list
> > items. Any ideas on how to do that?
>
> > thanks



[jQuery] Re: select text for LI

2007-11-02 Thread Jeffrey Kretz

Unfortunately, the text method by default returns the text of the element
and all its children (not just on lists but all types of elements).

This is similar to the IE method innerText.  It's a bit roundabout, but
maybe you could grab the innerHTML and then regex out the child contents.

Regex doesn't handle nested elements very well, but if you remove the LIs
first then the ULs it seems to work.  Here is a sample:

$(document).ready(function()
{
  $('li').each(function(i)
{
  var html = $(this).html();
  var text =
html.replace(//gi,'').replace(//gi,'');
  alert(text);
});
});

It worked on my test page, but I'm not sure if it will handle multiple
levels of nested lists properly.

If you're sure that your content will never have any brackets in it, you
could you this real greedy regex instead:

  var text = html.replace(/<[\s\S]+>/gi,'');

JK

-Original Message-
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Andy Matthews
Sent: Friday, November 02, 2007 9:52 AM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: select text for LI


Isn't that invalid HTML? Should it be this instead?


  index.html
  about
  
index.html
more.html
  
 

-Original Message-
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of sawmac
Sent: Friday, November 02, 2007 10:55 AM
To: jQuery (English)
Subject: [jQuery] select text for LI


I'm trying to select text inside list items. I'm using jQuery's .text( )
method. Unfortunately, that returns the text of all children as well. That
means for a nested list like this


  index.html
  about

  index.html
  more.html

  


$('li').eq(1).text() returns
'about
   index.html
   more.html'

I just want to retrieve "about" not the text from the child list items. Any
ideas on how to do that?

thanks





[jQuery] Re: select text for LI

2007-11-02 Thread Jonathan Sharp
I'm sure there's a more elegant solution but you could do something like:

// Untested
var about = $( 'selector for the li' ).clone().find('>
ul').remove().end().html();

Cheers,
-Jonathan


On 11/2/07, sawmac <[EMAIL PROTECTED]> wrote:
>
>
> I'm trying to select text inside list items. I'm using
> jQuery's .text( ) method. Unfortunately, that returns the text of all
> children as well. That means for a nested list like this
>
> 
> index.html
> about
>
>  index.html
>  more.html
>
> 
> 
>
> $('li').eq(1).text() returns
> 'about
>   index.html
>   more.html'
>
> I just want to retrieve "about" not the text from the child list
> items. Any ideas on how to do that?
>
> thanks
>
>


[jQuery] Re: select text for LI

2007-11-02 Thread Andy Matthews

never mind. I just ran a validate on that snippet of code and yours is the
valid markup. 

-Original Message-
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of sawmac
Sent: Friday, November 02, 2007 10:55 AM
To: jQuery (English)
Subject: [jQuery] select text for LI


I'm trying to select text inside list items. I'm using jQuery's .text( )
method. Unfortunately, that returns the text of all children as well. That
means for a nested list like this


  index.html
  about

  index.html
  more.html

  


$('li').eq(1).text() returns
'about
   index.html
   more.html'

I just want to retrieve "about" not the text from the child list items. Any
ideas on how to do that?

thanks




[jQuery] Re: select text for LI

2007-11-02 Thread Andy Matthews

Isn't that invalid HTML? Should it be this instead?


  index.html
  about
  
index.html
more.html
  
 

-Original Message-
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of sawmac
Sent: Friday, November 02, 2007 10:55 AM
To: jQuery (English)
Subject: [jQuery] select text for LI


I'm trying to select text inside list items. I'm using jQuery's .text( )
method. Unfortunately, that returns the text of all children as well. That
means for a nested list like this


  index.html
  about

  index.html
  more.html

  


$('li').eq(1).text() returns
'about
   index.html
   more.html'

I just want to retrieve "about" not the text from the child list items. Any
ideas on how to do that?

thanks