Hi All,

Wondering if anyone can help me out with this...

I have a list like this and want to select the "innermost" text from each LI:

<li>Item One</li>
<li><a href="yadda">Item Two</a></li>
<li><div class="something"><a href="ya">Item Three</a></div></li>
<li><a href="ya">Item Four</a></li>
<li>Item Five</li>

I want to be able to perform an operation using each result (the results being "Item One", "Item Two", "Item Three", "Item Four", "Item Five"). If the LI's all have an <a> tag inside of them, I've found that this works:

var text;
$('#myList li').each(function(){
  text = $('*',this).html();
  // do something using the text
});

But that only works for cases Two and Four in my example above (not the cases where the text is bare inside the LI or there's an additional wrapper in the LI. I'm thinking I'll need to do something recursively using the content of each LI or maybe there are some existing jquery selectors that can do what I'm looking for.

Any ideas?

Oh, if this got a little more fancy (or was easier to do), if we had a case like this, it would get only the "first" text it came across ("Item Six"):

<li>Item Six<div class="something"><a href="ya">Some text</a></div></li>

Thanks,
Jack


Reply via email to