3gwebtrain
3gwebtrain

Reputation: 15309

How to make a link, the text from object key and link href as object value.. using Handlebarsjs?

In my object i have a data like this :

naviObj = {"Home":"homelink","Service":"sericeLink","Contact":"contactLink"}

from this, i should make a link text from key's of object and links href should be the value of each key.. i tried this way..

naviObj = {"Home":"homelink","Service":"sericeLink","Contact":"contactLink"}

var naviMaker = function(){
    var naviHtml = Handlebars.compile($("#navigation").html());
    $("nav ul").append(naviHtml(naviObj));
}

naviMaker();

I am looking the result should be :

<ul>
<li><a href="homelink">Home</a></li>
<li><a href="sericeLink">Service</a></li>
<li><a href="contactLink">Contact</a></li>
</ul>

I am getting the link text as well the object value.. but i don't know to ket the key to text of link..

any one help me to solve this please..?

here is the jsfiddle

On google search i got an answer here : enter link description here, still any one give short approach appreciated. thanks.

Upvotes: 0

Views: 431

Answers (1)

Usual Suspect
Usual Suspect

Reputation: 601

Here is the Solution.JFIDDLE http://jsfiddle.net/ganeshprabhu1994/4qnaM/

HTML

<ul></ul>

Script

      $(function()
  {
      naviObj = {"Home":"homelink","Service":"sericeLink","Contact":"contactLink"};
      for(property in naviObj)
      {

          $('ul').append("<li><a href='"+naviObj[property]+"'>"+property+"</a></li>");
      }
  });

Upvotes: 1

Related Questions