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.