1

I want to add one more li at last but using JavaScript/jQuery

for example i want to add this li at last <li><a href="#header" >Back to top</a></li>

<ul id="nav">
    <li><a href="#nowhere" >Lorem</a></li>
    <li><a href="#nowhere" >Aliquam</a></li>
    <li><a href="#nowhere" >Morbi</a></li>
    <li><a href="#nowhere" >Praesent</a></li>
    <li><a href="#nowhere" >Pellentesque</a></li>
        Here i want to add one more li using javascript
</ul>
1

2 Answers 2

4
$(document).ready( function(){
    $('ul#nav').append('<li><a href="#header">Back to top</a></li>');
}  
Sign up to request clarification or add additional context in comments.

3 Comments

Assuming you have your HTML all set, including the jQuery library, you can test it interactively by typing just the actual append() line into the firebug command line. Once it works the way you want, paste the block as above into a <script> block in your <head> area.
but i'm getting this error in console "ReferenceError: append is not defined { message="append is not defined", more...}"
If you've got firebug, your HTML looks as it does in your question, and you are linking to a copy of jQuery, pasting this into the firebug command line: $("#nav").append('<li><a href="#header">Back to top</a></li>'); and hitting Return will work (just tried it).
4

Use the append function.

$("#nav").append('<li><a href="#header">Back to top</a></li>');

2 Comments

Like in baseball, two outfielders yelling "I got it!" on an easy pop fly.
one more +1 for jsFiddle link.

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.