I have this HTML on my page:
<div class="phrase">
<ul class="items">
<li class="agap"><ul><li>TEXT1</li></ul></li>
<li class="agap"><ul> </ul></li> <!-- empty ul -->
<li class="aword">TEXT2</li>
..
</ul>
</div>
<div class="phrase"> ... </div>
I would like to get for each “phrase” all the elements in “items” in a text variable, like this:
var string = "TEXT1 - BLANK - TEXT2";
I currently have this javascript code:
<script>
$(function() {
$('.phrase .items').each(function(){
var myText = "";
// At this point I need to loop all li items and get the text inside
// depending on the class attribute
alert(myText);
});
};
</script>
How can I iterate all <li> inside .items?
I was trying different ways but I didn’t get good results.
First I think you need to fix your lists, as the first node of a
<ul>must be a<li>(stackoverflow ref). Once that is setup you can do this:Working jsfiddle.
One thing is if you get the
.text()of an upper levelliyou will get all sub level text with it.Keeping an array will allow for many multiple phrases to be extracted.
EDIT:
This should work better with an empty
ULwith noLI: