moappi / json2html

Json2html is a lightning fast client side javascript HTML templating library with wrappers for both jQuery and Node.js.
https://www.json2html.com
MIT License
626 stars 104 forks source link

cant assign dynamic id for divs #14

Closed fr3em1nd closed 10 years ago

fr3em1nd commented 10 years ago

hi i get error on assigning dynamic ids on divs from the json data passed. this is useful for creating individual function for each divs

seebrown commented 10 years ago

How exactly are you assigning dynamic id's? Here are a few ways to do exactly that

Generate the unquie id in JSON and just use it in the div as the id

{"tag":"div","id":"${someUniqueID}"}

Use the index of the object in the array being transformed

{"tag":"div","id":function(obj,index) {
           return("div" + index);
}}

OR make up a unquie id on the spot

function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
               .toString(16)
               .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
         s4() + '-' + s4() + s4() + s4();
}

{"tag":"div","id":function() {return guid();}}
fr3em1nd commented 10 years ago

this helped thank you