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
Post a Comment