Its Wawan
Its Wawan

Reputation: 599

jquery explode how to do that

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>

Upvotes: 0

Views: 274

Answers (3)

bwicklund
bwicklund

Reputation: 144

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('') );

Upvotes: 1

elclanrs
elclanrs

Reputation: 94131

$('#myTags').append('<li>'+ exploded.join('</li><li>') +'</li>');

Upvotes: 5

bondythegreat
bondythegreat

Reputation: 1409

var stringLI = "";
for (i=0;i<exploded.length;i++) {
   stringLI += "<li>"+exploded[i]+"</li>";
}
$("#myTags").append(stringLI);

Upvotes: 1

Related Questions