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
?
If you are simply adding text in that
li
, you can use:This would do it:
Two things:
<li>
to the<ul>
itself.Instead of
try
This is the shortest way you can do that
Where blocks in an array. and you need to loop through the array.
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:
Happy coding :)