572

I have code that looks like this:

<div id="header">
    <ul class="tabs">
        <li><a href="/user/view"><span class="tab">Profile</span></a></li>
        <li><a href="/user/edit"><span class="tab">Edit</span></a></li>
    </ul>
</div>

I'd like to use jQuery to add the following to the list:

<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>

I tried this:

$("#content ul li:last").append("<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>");

But that adds the new li inside the last li (just before the closing tag), not after it. What's the best way to add this li?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eileen
  • 6,630
  • 6
  • 28
  • 29

14 Answers14

853

This would do it:

$("#header ul").append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');

Two things:

  • You can just append the <li> to the <ul> itself.
  • You need to use the opposite type of quotes than what you're using in your HTML. So since you're using double quotes in your attributes, surround the code with single quotes.
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 21
    just as a side note, you can use prepend instead of append in case you want to add as the first element instead of the last – Thomas May 14 '14 at 08:00
528

You can also do it in a more 'object' and still easy-to-read way:

$('#content ul').append(
    $('<li>').append(
        $('<a>').attr('href','/user/messages').append(
            $('<span>').attr('class', 'tab').append("Message center")
)));

You don't have to fight with quotes then, but you must keep trace of braces :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
57

If you are simply adding text in that li, you can use:

 $("#ul").append($("<li>").text("Some Text."));
p.campbell
  • 98,673
  • 67
  • 256
  • 322
kravits88
  • 12,431
  • 1
  • 51
  • 53
55

How about using "after" instead of "append".

$("#content ul li:last").after('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');

".after()" can insert content, specified by the parameter, after each element in the set of matched elements.

satomacoto
  • 11,349
  • 2
  • 16
  • 13
30

jQuery comes with the following options which could fulfil your need in this case:

append is used to add an element at the end of the parent div specified in the selector:

$('ul.tabs').append('<li>An element</li>');

prepend is used to add an element at the top/start of the parent div specified in the selector:

$('ul.tabs').prepend('<li>An element</li>');

insertAfter lets you insert an element of your selection next after an element you specify. Your created element will then be put in the DOM after the specified selector closing tag:

$('<li>An element</li>').insertAfter('ul.tabs>li:last');
will result in:
<li><a href="/user/edit"><span class="tab">Edit</span></a></li>
<li>An element</li>

insertBefore will do the opposite of the above:

$('<li>An element</li>').insertBefore('ul.tabs>li:last');
will result in:
<li>An element</li>
<li><a href="/user/edit"><span class="tab">Edit</span></a></li>
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
18

You should append to the container, not the last element:

$("#content ul").append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');

The append() function should've probably been called add() in jQuery because it sometimes confuses people. You would think it appends something after the given element, while it actually adds it to the element.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
7
$("#content ul").append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');
acrosman
  • 12,814
  • 10
  • 39
  • 55
Evan Meagher
  • 4,517
  • 21
  • 18
  • An explanation would be in order. For instance, [user *"Dipak"* wrote](https://stackoverflow.com/questions/1145208/how-to-add-a-list-item-to-an-existing-unordered-list#comment14077672_1145215): *"Shouldn't it be '#header' instead of '#content'?"* – Peter Mortensen Jun 05 '21 at 19:05
7

Instead of

$("#header ul li:last")

try

$("#header ul")
Adrian Godong
  • 8,802
  • 8
  • 40
  • 62
7

Use:

$("#content ul").append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');

Here is some feedback regarding code readability (shameless plug for a blog). http://coderob.wordpress.com/2012/02/02/code-readability

Consider separating the declaration of your new elements from the action of adding them to your UL.. It would look something like this:

var tabSpan = $('<span/>', {
    html: 'Message Center'
});
var messageCenterAnchor = $('<a/>', {
    href='/user/messages',
    html: tabSpan
});
var newListItem = $('<li/>', {
    html: messageCenterAnchor,
    "id": "myIDGoesHere"
});    // NOTE: you have to put quotes around "id" for IE..

$("content ul").append(newListItem);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undeniablyrob
  • 1,339
  • 2
  • 16
  • 16
  • @jmogera, are you trying to set the id? If so, you can just do that inside the { }'s. I updated the code above :) – undeniablyrob May 08 '12 at 18:36
  • The link is (effectively broken): *"Oops! That page can’t be found."* – Peter Mortensen Jun 05 '21 at 19:10