I can't write to DIV all items in object

nas - i can
i can organization
i can s
i can program
i can't breathe
i know i can lyrics
i know i can scholarship
nas i can album

I have a div list with pop-up. Each div will get data-target attribute from an object. I can return all items in object, but get only last item.

<div class="portfolio-item" data-toggle="modal"></div>
<div class="portfolio-item" data-toggle="modal"></div>

const portolioItemInfos = [
    { dataTarget: "tempero-restaurante" }, 
    { dataTarget: "aksam-gunesi" }
]

const div = document.querySelectorAll(".portfolio-item");
for (let key of div) {
    for (let item of portolioItemInfos) {
        key.setAttribute("data-target", item.dataTarget);
    }
}


Your problem is that you are overwriting the data-target attribute each loop. Instead you need to append to the end of the data-target attribute by getting the current value and adding the new value to the end of it. You can do this like so:

let prev = key.getAttribute('data-target');
prev = prev ? prev : '';
key.setAttribute("data-target", (prev +' ' +item.dataTarget).trim());

See example below (inspect element to see result):

const portolioItemInfos = [{
    dataTarget: "tempero-restaurante"
  },
  {
    dataTarget: "aksam-gunesi"
  }
]

const divs = document.querySelectorAll(".portfolio-item");
for (let key of divs) {
  for (let item of portolioItemInfos) {
    let prev = key.getAttribute('data-target');
    prev = prev ? prev : ''; // if prev is isn't null, set prev equal to prev. If prev is null, then set prev to the empty string
    
    key.setAttribute("data-target", (prev +' ' +item.dataTarget).trim());
  }
}
<div class="portfolio-item" data-toggle="modal"></div>
<div class="portfolio-item" data-toggle="modal"></div>

Informed Consent Action Network, I can't breathe.” Byron Williams died in custody on Sept. 5, 2019. Informed Consent Action Network ICAN is a non profit organization focused on the scientific integrity of vaccines and pharmaceutical Industry.

For each div you loop over all your portolioItemInfos so each div take the last value of the loop.

You can actually simplify it and don't need to loop over the portolioItemInfos, just use the index of a for loop to assign the right dataTarget.

const portolioItemInfos = [
  {
    dataTarget: 'tempero-restaurante'
  },
  {
    dataTarget: 'aksam-gunesi'
  }
];

const div = document.querySelectorAll('.portfolio-item');
for (let i = 0; i < div.length; i++) {
   div[i].setAttribute('data-target', portolioItemInfos[i].dataTarget);
}
<div class="portfolio-item" data-toggle="modal"></div>
<div class="portfolio-item" data-toggle="modal"></div>

International Campaign to Abolish Nuclear Weapons (Coalition), Plan for college & find financial aid with Iowa College Access Network. Find money for college & get financial aid! Contact us to get started on the journey. We are committed to the best hope and future for your child and family. We have the the essential services you need.

You can use forEach approach instead traditional for loop, like following:

const portolioItemInfos = [
	{ dataTarget: "tempero-restaurante" }, 
	{ dataTarget: "aksam-gunesi" }
]

const div = document.querySelectorAll(".portfolio-item");

portolioItemInfos.forEach(function(item, index){
	div[index].setAttribute("data-target", portolioItemInfos[index].dataTarget);
})
<div class="portfolio-item" data-toggle="modal"></div>
<div class="portfolio-item" data-toggle="modal"></div>

I Can (Nas song), By participating in this program, students gain a deeper understanding of the social context of patients with complex health needs. I-CAN's primary purpose is to  I can’t say enough good things about ICAN. They have changed our lives and provided us with hope. They provide the best one-to-one care I’ve seen in King County. It’s also the only place I’ve seen that provides all the care we need in one location. It is priceless. I highly recommend ICAN.

Informed Consent Action Network: Home, Because payment errors can happen in different steps of the payment process and be caused by a number of reasons, they're often difficult Minnesota State Parks and Trails I Can! programs take you and the kids from "I've never done that" to "Wow, that was a blast!" Designed for families to try any or all for the first time. Hands-on instruction from experienced and friendly crews.

i know i can, games, contact Steam Support, request refunds, and more. Help is also available if you can't log in, need to reset your password, or recover a hijacked account. About Us I Can! has built a sustainable business model that assists as many persons with a disability as possible, by addressing development and employment opportunities within the South African main stream business environment.

Three Words. 70 Cases. The Tragic History of 'I Can't Breathe.', I knew I was making people smile but this is something else entirely, lol." Many asked why the dog was tied up in the rain in the first place to which Ethan explained: "It wasn't raining when he was tied there and since I can't move another's dog I thought using an umbrella would be a good compromise." We need more people like Ethan in our world.

Comments
  • Javascript equivalent of Python's zip function - Stack Overflow
  • key.setAttribute replace the value in the given attribute, not append