Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Dynamically called function, argument not defined

$
0
0

I am dynamically generating a gallery, each gallery item has an onClick event that calls a function which has the items value as argument.

I tested the code without the "" and the value is defined but the function runs when the item is created and not when clicked.

When I use the "" the function only runs when I click it but I get value not defined

I would like to dynamically create the function, and trigger it on click using the value as argument.

jsGallery is an array that I'm cycling to build the gallery items.

Gallery Item code

function createDiv(type) {
  jsGallery.forEach((item) => {
    if (item.type === type) {
      let newDiv = document.createElement("div");
      newDiv.setAttribute("value", item.id);
      newDiv.setAttribute("class", "item" + "" + type);
      newDiv.setAttribute("id", "dynamic");

      newDiv.setAttribute("onClick", "testFire(value)");

      document.getElementById("galleryContainer").appendChild(newDiv);
    }
  })
}

TestFire function

function testFire(value) {
  console.log(value);
}

Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>