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 ?

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Simon got it first, but your solution is much more elegant! And it has a jsFiddle!.
I don't see it is more elegant. It does the same. At least it adds divs with the same class names.
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
accepted because you have chuck norris as your name and the example helped me alot! :)
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

Comments

1
$("#test-ul").append("html here");    // after the last li

$("#test-ul").prepend("html here");   // before the first

Comments

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);
});

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.