0

I have to generate some li element automatically and the way I was doing it it through a function that return text inside a loop, something like this:

function getLi(data) {
   return '<li>' + data + '</li>';
}

then I found a better way to do it by writing html inside a div:

<div style="display:none;" id="Template">
   <li id="value"></li>
</div>

and then I would change the id and value get the html and reset the element to original state:

var element = $("#value");
element.html(data);
element.attr('id', getNewId());
var htmlText = $("#Template").html();
element.html('');
element.attr('id', 'value');
return htmlText;

then I was reading on script template

and I figured this could be a better way of doing it,
However apply the previous code didn't work as the inner elements didn't exist according to this article

so how can I apply this?

EDIT: I put inside a ul tag, I use this method to get the items dynamically

EDIT2:

<li>
   <a href="#" >
   <span>
   <span>
some text
   </span>
   </span>
</li>

this isn't necessarily what I have but something along the way

Edit3: my ul does not exist orgialy it's generated dynamically I insist this is not a duplicate I want to know how to use a template with some dynamic variables

Alaa Jabre
  • 1,843
  • 5
  • 26
  • 52
  • The 1st way is better, but it needs to be modified. By the way, what do you do with the generated LI? It'd be good get a bit more of context. – lshettyl Mar 21 '15 at 10:12
  • Right, I have posted my take. Take a look. – lshettyl Mar 21 '15 at 10:31
  • possible duplicate of [jQuery: how to add
  • in an existing
      ?](http://stackoverflow.com/questions/1145208/jquery-how-to-add-li-in-an-existing-ul)
  • – tawfekov Mar 21 '15 at 11:27
  • @tawfekov Not a duplicate, the question is clear I want to use template – Alaa Jabre Mar 21 '15 at 11:30
  • Why don't you use http://www.handlebarsjs.com/ ? – Dabbas Mar 21 '15 at 15:07