Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:29:38+00:00 2026-05-22T11:29:38+00:00

I have this HTML on my page: <div class=phrase> <ul class=items> <li class=agap><ul><li>TEXT1</li></ul></li> <li

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:29:39+00:00Added an answer on May 22, 2026 at 11:29 am

    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:

    // note this array has outer scope
    var phrases = [];
    
    $('.phrase').each(function(){
            // this is inner scope, in reference to the .phrase element
            var phrase = '';
            $(this).find('li').each(function(){
                // cache jquery var
                var current = $(this);
                // check if our current li has children (sub elements)
                // if it does, skip it
                // ps, you can work with this by seeing if the first child
                // is a UL with blank inside and odd your custom BLANK text
                if(current.children().size() > 0) {return true;}
                // add current text to our current phrase
                phrase += current.text();
            });
            // now that our current phrase is completely build we add it to our outer array
            phrases.push(phrase);
        });
        // note the comma in the alert shows separate phrases
        alert(phrases);
    

    Working jsfiddle.

    One thing is if you get the .text() of an upper level li you 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 UL with no LI:

    // outer scope
    var phrases = [];
    
    $('.phrase').each(function(){
        // inner scope
        var phrase = '';
        $(this).find('li').each(function(){
            // cache jquery object
            var current = $(this);
            // check for sub levels
            if(current.children().size() > 0) {
                // check is sublevel is just empty UL
                var emptyULtest = current.children().eq(0); 
                if(emptyULtest.is('ul') && $.trim(emptyULtest.text())==""){
                    phrase += ' -BLANK- '; //custom blank text
                    return true;   
                } else {
                 // else it is an actual sublevel with li's
                 return true;   
                }
            }
            // if it gets to here it is actual li
            phrase += current.text();
        });
        phrases.push(phrase);
    });
    // note the comma to separate multiple phrases
    alert(phrases);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple HTML page with svg like this: <div id=plan> <svg xmlns=http://www.w3.org/2000/svg version=1.1>
I have this HTML in my page <div id='masterDIV'> <div id='divItemNumber'>23</div> <div id='divDesc'>This is
I have this script on my html page: <script language='javascript'>parent.resizeTo(550,510);</script> I'd like to add
I have the following html page: <body> <div class=hide1 style=width:1000px; height:1000px;> <table width=100% border=0
I have this html: <div id=signbtn> <a id=login-link class=btnsignin title=Login>Sign in</a> </div> Upon successful
I have this piece of html in my page. <p><a href=# id=addQueryPart >Add</a></p> <div
I have this piece of html in my page. <p><a href=# id=addQueryPart >Add</a></p> <div
I have the following HTML in a page feature: <div id=feature-detail> <div class=da1 d-active>
I have this html page which is very simple, it contains a text box
I have an html page i want to print a portion of this html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.