Solved

Jquery creating list from array

Posted on 2015-02-19
3
288 Views
Last Modified: 2015-02-19
Hello experts.
I'm stucking with my list i want to create from one array.
The problem is as follow
Usig the following code:
var cList = $('#catlist');
var catValues = new Array();
$('#myholder').find('li.ui-selected').each(function() {
   catValues.push($(this).text());
});
$.each(catValues, function(i) {
 var li_a = $('<li/>').appendTo(cList);
 var span = $('<span/>').addClass('test').text(catValues[i]).appendTo(li_a);
})

Open in new window

i get the output like:
<ul id="catlist">
 <li>
 <span class="test">my text</span>
</li>
</ul>

The result i need is:
<ul id="catlist">
 <li>
 <span class="test"></span>my text
</li>
</ul>

(the text after the span)
Any help please??
0
Comment
Question by:Panos
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 40620256
Hi,
here we go:
    var cList = $('#catlist');
    var catValues = new Array();
    $('#myholder').find('li.ui-selected').each(function() {
       catValues.push($(this).text());
    });
    catValues.push('Test One');
    catValues.push('Test Two');
    catValues.push('Test Three');
    catValues.push('Test Four');
    $.each(catValues, function(i) {
         var li_a = $('<li/>').appendTo(cList);
         var span = $('<span/>').addClass('test').appendTo(li_a);
         li_a.append(catValues[i]);
    });

Open in new window


Sample:
http://jsfiddle.net/EE_RainerJ/28fgr4rd/

HTH
Rainer
0
 
LVL 2

Author Comment

by:Panos
ID: 40620267
Thank you Rainer Jeschor
Sometimes the solution is in front of me and i don't see it.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 40620268
regards
panos
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

776 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