Answer the question
In order to leave comments, you need to log in
How to replace jQuery HTML with data from an array?
Due to insufficient knowledge, I have been racking my brain for 2 days on how to implement this. There is an array:
var array = [
{id:1,name:"name1",img:"/images/img1.jpg",url:"http://url.ru/1"},
{id:2,name:"name2",img:"/images/img2.jpg",url:"http://url.ru/2"}
];
jQuery('.name').html('Николай');
Answer the question
In order to leave comments, you need to log in
var id, name, img, url;
var array = [
{id:1,name:"name1",img:"/images/img1.jpg",url:"http://url.ru/1"},
{id:2,name:"name2",img:"/images/img2.jpg",url:"http://url.ru/2"}
];
var d = $('div').data('price');
var arr = d.split("'");
$.each(array,function(index,value){
if (arr[1] == value["id"]){
id = value["id"];
name = value["name"];
img = value["img"];
url = value["url"];
};
console.log('id: ' + id + '; name: ' + name + '; img: ' + img + '; url: ' + url);
});
Just loop through the array, substituting data into your script from the current value.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question