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 8389241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:40:24+00:00 2026-06-09T18:40:24+00:00

I’m trying to create an unordered list in javascript. My code reads: $(div).append(<ul>) for

  • 0

I’m trying to create an unordered list in javascript. My code reads:

$(div).append("<ul>")
for (i in usersLst){
    $(div).append("<li>")
    $(div).append(usersLst[i][1])
    $(div).append("</li>")
}
$(div).append("</ul>")

Then the result is:

•
bob
•
alice
•
fred

However, if the code reads:

$(div).append("<ul>")
for (i in usersLst){
    $(div).append("<li>"+usersLst[i][1]+"</li>")
}
$(div).append("</ul>")

Then the result is:

 - bob
 - alice
 - fred

So with three separate appends it appears that a newline is being mysteriously inserted. What’s going on?

  • 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-06-09T18:40:26+00:00Added an answer on June 9, 2026 at 6:40 pm

    Assume this code:

    $('div')
        .append("<ul>")
        .append("<li>");
        .append('foo');
        .append("</li>")
        .append("</ul>")​​​
    

    Lets have a look at the resulting structure (Chrome 21):

    <div>
        <ul></ul>
        <li></li>
        foo
    </div>
    

    What happend? .append takes each argument and converts the strings to proper DOM elements. Thus the code is the same as doing:

    $('div')
       .append(document.createElement('ul'))
       .append(document.createElement('li'));
       .append(document.createTextNode('foo'));
    

    The two calls containing closing tags are ignored since they cannot be convert to valid HTML / DOM elements.

    .append (and all other DOM manipulation methods) is working on DOM elements. It’s just jQuery’s way of calling .appendChild [MDN].

    HTML is just a specific format of representing structure. In there, each element is represented by a opening tag and an (optional) closing tag. The browser is parsing the HTML and creates the DOM in memory. The DOM (Document Object Model) is a well defined interface for interacting with hierarchical, structured data. Once you are working with the DOM, start and end tags don’t exist anymore (that’s HTML), only Nodes. I recommend to read about DOM on MDN.

    jQuery allows you to pass HTML strings to .append because it is convient, but each string is immediately converted to corresponding DOM nodes and appended to other nodes. You cannot build an HTML string with multiple calls to .append.

    This is a corrected version of your code:

    // jQuery parses the HTML string and creates a UL element
    var $ul = $('<ul />');
    // equivalent to
    // var $ul = document.createElement('ul'); <- NOTE: not HTML, it's a node name
    
    for (var i in usersLst) {
        // jQuery parses the HTML, creates a LI element and appends it to the list
        $ul.append('<li>' + usersLst[i][1] + '</li>');
        // equivalent to
        // var li = document.createElement('li');
        // li.appendChild(document.createTextNode(usersLst[i][1]));
        // $ul.appendChild(li);
    }
    
    // append the populated UL element to an existing node:
    $(div).append($ul);
    // equivalent to
    // existingElement.appendChild($ul);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.