Thursday, May 26, 2011

Add an element before the last element of it's kind in jQuery wiht insertBefore()


// Add the new tag (li element) before the last tag (li element)
      
$('<li>this is a test 3</li>').insertBefore($('li').last());

In an unordered list such as

<ul>
   <li>this is a test 1</li>
   <li>this is a test 2</li>
   <li>this is a test 4</li>  <------ last element
</ul>

The above jQuery statement will insert it before the last li element -

<ul>
<li>this is a test 1</li>
<li>this is a test 2</li>
<li>this is a test 3</li>  <------ inserted here
<li>this is a test 4</li>  <------ still the last element
</ul>


http://api.jquery.com/insertBefore/

4 comments:

Name: Andrew said...

Used http://www.simplebits.com/cgi-bin/simplecode.pl?mode=process for Blogger code display

Anonymous said...

скачать opera mini для Nokia610

SteveBrad said...

Thanks for the nice blog. It Was Very useful for me. Keep Such sharing ideas in the Future as well. Actually This Was What I Was looking for, and I am glad to cam here ! Thanks for Sharing the information with us.
Generic Viagra

Unknown said...

Your blog was tweeted by a friend yesterday evening. Thought I’d take a look. Best decision ever.I will keep your new article.

wholesale cheap jerseys
nfl jerseys cheap
cheap mlb jerseys