jquery - JavaScript object properties - connect them together? -


i have 3 objects in array.

myarray = [ {name: "iphone", link: "www.apple.com"}, {name: "nokia", link: "www.nokia.com"}, {name: "google", link: "www.google.com"} ] 

how make loop , put properties display on page, this:

iphone nokia google

and have links when click on them

thanks!

with pure js, :

for(var i=0;i<myarray.length;i++){     var = document.createelement('a');     a.href= myarray[i].link;     a.text= myarray[i].name;     document.body.appendchild(a); } 

but easier jquery:

you can use jquery .each(). loop through array , can access object properties this.

to make link, create a element , assing value .attr() , .text()

$.each(myarray, function(){     var = $('<a/>'); //the element     a.attr('href', this.link) //the href     a.text(this.name) //the text     $('body').append(a) //append body }) 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -