-1

So I'm looping through a JSON object and rendering the data in the View. Each object also has a button that I created. When I call the idea of that button to do some action only the first button works. Here is html code:

<div class="row text-center" style="display:flex; flex-wrap: wrap;">
    <% data["Search"].forEach((movie) => { %>
      <div class="col-md-3 col-sm-6">
        <div  class="thumbnail">
          <div class="caption">
            <li><%= movie["Title"] %></li>
            <li><%= movie["Year"] %></li>
            <li id="id"><%= movie["imdbID"]%></li>
          </div>
          <img  src="<%= movie["Poster"] %>" alt="">
          <button id="clickable"  type="button" name="button">Get More details About The Movie</button>
        </div>
      </div>
    <% }) %>
</div>
</div>

As you can the button has id="clickable". Here is the code for jQuery:

<script  type="text/javascript">
$(document).ready(function(){
//alert(this).data("#hello");
$( "#clickable" ).click(function() {
  var item = $("#id").text();
  alert('Selected Name = ' + item );
});
});

For now I'm just displaying id="id" data. Assume that looping through 10 different movies but only the first one works when I click id="clickable".

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rocky Singh
  • 629
  • 5
  • 12
  • Again, use a class and contextual lookups. http://learn.jquery.com/using-jquery-core/selecting-elements/ – Taplar Feb 28 '18 at 00:01
  • Okay, I kinda got it. Do you mind explaining how contextual lookups work with classes? @Taplar – Rocky Singh Feb 28 '18 at 00:05