Reputation: 3
This is using JavaScript. I dynamically generate a list of movies using omdb API and use li and a elements to each movie name.
After clicking one of the movies in the list, I want to fetch the image using the id of the movie.
What should I use to get the id attribute of the clicked element li. Currently the click event listener is set on the class of the movie list on not on each li since its dynamically generated.
function getPoster(e){
e.preventDefault();
//var id = document.getElementbyId("id");
var url = "http://www.omdbapi.com/?i=" + id;
fewd.getJSON(url, updatePoster);
}
Upvotes: 0
Views: 100
Reputation: 54791
The event object has the property target
that references the DOM element that triggered the event.
function getPoster(e){
e.preventDefault();
var url = "http://www.omdbapi.com/?i=" + e.target.id;
fewd.getJSON(url, updatePoster);
}
Upvotes: 2