[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Jquery creating list from array

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
Panos
Asked:
Panos
  • 2
1 Solution
 
Rainer JeschorCommented:
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
 
PanosAuthor Commented:
Thank you Rainer Jeschor
Sometimes the solution is in front of me and i don't see it.
0
 
PanosAuthor Commented:
regards
panos
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now