jQuery append to bottom of list
Asked Answered
S

2

11

Can someone lend me a hand

I have this unordered list

<ul id="nav">
<li><a href="whatwedo.aspx">WHAT WE DO</a>
<ul>
    <li><a href="development.aspx">Development</a></li>
    <li><a href="marketassessment.aspx">MARKET ASSESSMENT AND CONCEPT DEVELOPMENT</a></li>
    <li><a href="planning.aspx">DEVELOPMENT PLANNING AND OVERSIGHT</a></li>
    <li><a href="preopening.aspx">PRE-OPENING OPERATIONAL SERVICES</a></li>
    <li><a href="operations.aspx">OPERATIONAL MANAGEMENT SERVICES</a></li>
    <li><a href="turnaround.aspx">TURNAROUND SERVICES</a></li>
    <li><a href="news.aspx">NEWS</a></li>
</ul>
</li>
<li><a href="ourparks.aspx">OUR PARKS</a></li>
<li><a href="contact.aspx">CONTACT US</a></li> </ul>

And I want to add a new list to the bottom of the list.

<li class="last_link"><a href="https://projects.parc-services.com" target="blank">Login</a></li>

Would I go about it by doing something like this?

$("#nav ul").prepend("<li></li>");
Saddlecloth answered 13/6, 2011 at 18:32 Comment(1)
The title of this answer is the solution :PGuayule
A
19

If you want to add at the end use the append() method instead of prepend():

$('#nav ul').append('<li class="last_link"><a href="https://projects.parc-services.com" target="blank">Login</a></li>');

or as I prefer:

$('#nav ul').append(
    $('<li/>', {
        'class': 'last_link',
        html: $('<a/>', {
            href: 'https://projects.parc-services.com',
            target: '_blank',
            text: 'Login'
        })
    })
);
Abbatial answered 13/6, 2011 at 18:34 Comment(1)
Thank you I was able to get to work with this: $("#nav_menu #nav").append('<li class="last_link"><a href="projects.parc-services.com"target="blank">Log…);Saddlecloth
R
10

prepend adds something at the beginning of an element. append is used to add something at the end.

$("#nav ul").append($("<li></li>").html('something'));

And if you want to add a class, or anything else you can:

$("#nav ul").append($("<li></li>").html('something')
                                  .addClass('myclass'));
Rambert answered 13/6, 2011 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.