0

i have a question for you...

for the first lets say i have a variable on javascript like this..

var string = 'a,b,c';
var exploded = string.split(',');

and then i have a html element like this

<ul id='myTags'>
</ul>

and my question is.. how to create / append to inside of my " ul id='myTags' " so it will be like this...

<ul id='myTags'>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
Wawan Brutalx
  • 603
  • 6
  • 15
  • 27

3 Answers3

5
$('#myTags').append('<li>'+ exploded.join('</li><li>') +'</li>');
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • You're on fire today: I think that's the third in a row that I just gave you a +1 for the best answer. – nnnnnn Nov 14 '12 at 04:06
1
var stringLI = "";
for (i=0;i<exploded.length;i++) {
   stringLI += "<li>"+exploded[i]+"</li>";
}
$("#myTags").append(stringLI);
bondythegreat
  • 1,379
  • 11
  • 18
1

Best way would be to only append once:

Jquery

var string = 'a,b,c';
var exploded = string.split(',');
var items = [];

$.each(exploded, function() {
    items.push('<li>'+this+'</li>');
}); 

$('#myTags').append( items.join('') );
bwicklund
  • 144
  • 1
  • 4