0

I have a menu:

<ul id="test-ul">
<li>test</li>
<li>test2</li>
</ul>

How do i add the following HTML markup before the first li and after the last li:

 <li class="first">
 <img src="http://i48.tinypic.com/sosmck.jpg" width="27" height="15" />
 </li>
<li class="last">
 <img src="http://i48.tinypic.com/sosmck.jpg" width="27" height="15" />
 </li>

using jquery ?

asked Jun 19, 2012 at 12:46
5
  • You should use the jquery append method to $("#test-ul") to add to the end, and the jquery prepend method to $("#test-ul") to add to the beginning: api.jquery.com/append api.jquery.com/prepend Commented Jun 19, 2012 at 12:47
  • I saw that apend does work but isnt it slow? Commented Jun 19, 2012 at 12:48
  • 4
    This will cause invalid markup. List elements (like ul) should contain only li elements inside. Commented Jun 19, 2012 at 12:48
  • ok how can i add an extra li? Commented Jun 19, 2012 at 12:49
  • @sp-1986 $("#test-ul").append("<li />"); $("#test-ul").prepend("<li />"); Commented Jun 19, 2012 at 12:50

5 Answers 5

4

This isn't valid syntax to add div in ul tag. But to undeerstand concept of adding html via Jquery, here you go

var str = '<div class="first"><img src="http://i48.tinypic.com/sosmck.jpg" width="27" height="15" /></div><div class="last"><img src="http://i48.tinypic.com/sosmck.jpg" width="27" height="15" /></div>';
$("#test-ul").append(str).prepend(str);​​​​​​​​

You can see Jsfiddle example here.

answered Jun 19, 2012 at 12:50
4
  • 1
    @Simon got it first, but your solution is much more elegant! And it has a jsFiddle!. Commented Jun 19, 2012 at 12:52
  • I don't see it is more elegant. It does the same. At least it adds divs with the same class names. Commented Jun 19, 2012 at 12:56
  • On second thoughts I think this might not be quite what the OP is after. I think the OP would wants the first element before the lis and the last element after the lis Commented Jun 19, 2012 at 12:58
  • 1
    accepted because you have chuck norris as your name and the example helped me alot! :) Commented Jun 19, 2012 at 14:48
2

You shouldn't add these elements before and after li. Any element other than li nested in a ul is invalid HTML.

You could however add the content within a new li, this would be valid.

​$(function(){
 var $li = $("<li></li>");
 $li.html("html code...");
 $("#test-ul").append($li);
 var $li2 = $("<li></li>");
 $li2.html("different html code..");
 $("#test-ul").prepend($li2); 
});​

SEE EXAMPLE

answered Jun 19, 2012 at 12:48
0
1
$("#test-ul").append("html here"); // after the last li
$("#test-ul").prepend("html here"); // before the first
VisioN
146k34 gold badges287 silver badges291 bronze badges
answered Jun 19, 2012 at 12:48
0
0

If you want to create a new LI for each, this would work. Be aware if you add more divs it will cause issues.

$('div').each(function() {
 var newli = $(this).html().wrap('<li></li>');
 $('#test-ul').append(newli);
});
answered Jun 19, 2012 at 12:51
0
var first = '<li<img src="/sosmck.jpg" width="27" height="15" /></li>';
var last = '<li><img src="/sosmck.jpg" width="27" height="15" /></li>';
var myItems = $("li");
myItems.prepend(first);
myItems.append(last);
answered Jun 19, 2012 at 12:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.