?
Solved

How do I insert li into ul based on an attribute?

Posted on 2010-08-25
5
Medium Priority
?
785 Views
Last Modified: 2012-05-10
Hi,
I have a <ul> like below and i need to insert a <li> based on msg-time attribute.
If msg-time=250, i need to insert it between msg-time 200 and 300

Need to insert
 <li  msg-time="250" class="msg-item"></li>

into
<ul id="msg-list">  
        <li  msg-time="100" class="msg-item"></li>
        <li  msg-time="200" class="msg-item"></li>
        <li  msg-time="300" class="msg-item"></li>  
        <li  msg-time="400" class="msg-item"></li>  
        <li  msg-time="500" class="msg-item"></li>  
</ul>

What is the best way? I may have many <li>, so I need to be efficient.

Thanks
Jamie
0
Comment
Question by:jamie_lynn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:johnsy32
ID: 33521106
You could select the list item with the msg-time attribute of 200, and then append the new element after..

$('[msg-time=200]').after('<li msg-time="250" class="msg-item"></li>');
0
 

Author Comment

by:jamie_lynn
ID: 33524195
Hi johnsy,

I don't know what the msg-time values in the <li> before hand. I can't hardcode the values

Thanks
Jamie
0
 
LVL 3

Expert Comment

by:johnsy32
ID: 33524373
Hi

You could loop through each li item, and then test the msg-time attribute on each element...

$('#msg-list li').each(function() {
                  
if ($(this).attr('msg-time') == 200) {
      $(this).after('<li msg-time="250" class="msg-item"></li>');
}
                                                            
});

I'm not completely sure what you are trying to do. How is the list being built?
0
 
LVL 3

Accepted Solution

by:
johnsy32 earned 2000 total points
ID: 33524515
Hey

If you mean that the 250 could be a variable and could change, and depending on what the value is, put it between the closest two numbers, you could do something like this.

put the msg_time into a variable
set the element to be added to the list
loop through the list
get the msg_time of the current list item
get the msg_time of the next list item
then test to see if msgtime is between these 2 numbers, if it is, add the new element

            msg_time = 250;
            new_list_element = ' <li msg-time="' + msg_time + '" class="msg-item">new</li> ';
            
            $('#msg-list li').each(function() {
                  
                  minimum = $(this).attr('msg-time');
                  maximum = $(this).next().attr('msg-time');
            
            
                  if(msg_time >= minimum && msg_time <= maximum) {
                      $(this).after(new_list_element);
                  }

                                                            
              });
0
 

Author Comment

by:jamie_lynn
ID: 33524557
That works great! Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question