4

Running the following code:

$('<div>').append('<ul>').append($('<li>').text('text'));

I would like to have

<div>
 <ul>
    <li>text</li>
 </ul>
</div>

What is wrong in my code?

7 Answers 7

4

$('<div>').append('<ul>') returns div jQuery object and so on. It is for something called chain of functions. Usually if you write in jQuery $(selector).method(...) it returns object you get by selector, so you can write $(selector).method1(...).method2(...), for example $(selector).css('color', '#555').text('hello') it sets color to the element and then change text into it.

var div = $('<div>');
var ul = $('<ul>').appendTo(div);
var li = $('<li>').text('text').appendTo(ul);

Shortly:

$('<div>').append($('<ul>').append($('<li>').text('text')));
Sign up to request clarification or add additional context in comments.

Comments

4

The order of the append functions is wrong...you are now appending the li to the div, next to the ul, not inside it.

This would be better:

var $myList = $('ul').append('<li>text</li>');
$('div').append($myList);

Comments

1
$('<div>').append($('<ul>').append($('<li>').text('text'))); 

Comments

1

If you just want the div and no need to have a reference to UL or LI this is a easy clear way :

$('<div><ul><li>text</li></ul></div>');

1 Comment

+1 this is the way it should be.. $("aParentObject").append('some html');
0

It starts from left to right, so you append ul to the div and then append li to the div.
You can fix this using the following.

var $ul = $('<ul>');
var $li = $('<li>').text('text');
$ul.append($li);
$('<div>').append($ul);

or in one line: $('<div>').append($('<ul>').append($('<li>').text('text')));

demo

Comments

0

As i see everybody first tries to walk around than does the job wanted..

This is ur html:

<div>
    <ul>
        <li>text</li>
    </ul>
</div>

And this is the code you should use

var MyString = "Write something here!"
$("someParent").append('<div> <ul> <li> ' + MyString + ' </li> </ul> </div>');

or just do

$("someParent").append('<div> <ul> <li> Text </li> </ul> </div>');

if you would like to add more than 1 list item than

with razor syntax and MVC:

var items = '@foreach(SelectListItem item in (System.Collections.IEnumerable)ViewData["MYItems"]){ <li> @item.Text </li> }';
$("someParent").append('<div> <ul>' + items + '</ul> </div>');

without razor syntax:

var items = "<li> A </li>" + 
            "<li> B </li>" + 
            "<li> C </li>" + 
            "<li> D </li>" +  
            "<li> E </li>";
$("someParent").append('<div> <ul>' + items + '</ul> </div>');

there are more than couple ways to do it just pick what you wanna do.

Comments

0

Simply use this

$("#Div_id").append('<ul>
                         <li>ListItem1</li>
                         <li>ListItem2</li>
                         <li>ListItem3</li>
                         <li>ListItem4</li>
                    </ul>');

like this if you put this append for an click event you can generate many lists as the clicks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.