0

I am really new with javascript

I am trying to loop my json then add the images to my html, then i need to make it so when i click on that image it shows me the File from my json

          var obj = JSON.parse(data);
          for (index = 0; index < obj.length; ++index) {
            
            var file = obj[index]['File'];
            var image = obj[index]['Image'];

            var test = $( ".row" ).append( 
              `<div class="column">
                <div class="card">
                  <img src="${image}" alt="Avatar" style="width:100%">
                  <div class="container">
                    <h4>
                      <b>Title</b>
                    </h4>
                  </div>
                </div>
              </div>`
              );

              test.click(function(e) {
                alert(file);
              });
  
              console.log(file);
              console.log(image);
            }
genibec746
  • 29
  • 4
  • Can you explain what the issue is? – laiju Nov 18 '21 at 05:18
  • for one i have no idea what im doing, if this is the correct way to di it. pluss when i click on the image i always get the last image added, and the message pops up 4 times (currently only have 4 items im adding) – genibec746 Nov 18 '21 at 05:22
  • check this https://stackoverflow.com/questions/1525664/jquery-how-to-bind-onclick-event-to-dynamically-added-html-element – laiju Nov 18 '21 at 05:28

0 Answers0