Get Paid to Participate - up to $1 per post!     Twitter     Facebook     Google+
Hosting Discussion
 

Hosting Discussion > Web Hosting Forums > Website Development & Design > Add new element to list using jQuery
forgot password?



Reply


Old
  Post #1 (permalink)   02-12-2010, 03:03 PM
HD Newbie
 
Join Date: Jan 2010
Posts: 10

Status: shakemelikeapig is offline
Hi,
I'm using jQuery on an app I've been developing. Let's say I have an unorded list like this...

Code:
<ul id="animals">
     <li rel="10">Cat</li>
     <li rel="20">Dog</li>
     <li rel="30">Bird</li>
     <li rel="40">Monkey</li>
</ul>
Then, lets say I have a variable called 'newPosition' whose value is numeric and comes from somewhere else (it doesn't matter where).

I basically want to put a new element into my #animals list so that it is positioned based on the value of the newPosition variable and that of the 'rel' attributes. So, in the example above, if newPosition = 15 then the new element (an li) should be placed between cat and dog.

I have this loop to loop through each li...

Code:
$("#animals").children().each(function() {
     relValue = $(this).attr("rel");
});
So... when the loop comes around to the right psoition how can I add a new element into the list?

Thanks
 
 
 
Reply

Thread Tools

New Post New Post   Old Post Old Post
Posting Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Sponsored By: