codingforfun
codingforfun

Reputation:

jquery append to front/top of list

I have this unordered list

<ul>
   <li>two</li>
   <li>three</li>
</ul>

Is there a way I can prepend to the unordered list so that it ends up like this?

<ul>
   <li>ONE</li>
   <li>two</li>
   <li>three</li>
</ul>

Notice the "ONE" is added to the FRONT/TOP of the list.

Upvotes: 110

Views: 91455

Answers (3)

Ishan Liyanage
Ishan Liyanage

Reputation: 2407

use prepend instread of append

<h2>Greetings</h2>    
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>

$('.container').prepend('<p>Test</p>');

refer http://api.jquery.com/prepend/ for more info.

Upvotes: 9

nezroy
nezroy

Reputation: 3186

Something simple like this ought to work:

$("ul").prepend("<li>ONE</li>");

Upvotes: 40

dasony
dasony

Reputation: 3708

$("ul").prepend("<li>ONE</li>");

Upvotes: 258

Related Questions