i want to insert div into my DOM, and in this div i have a button. so i want to create eventListener to this appended button, but i don't know how? i tried this but now work...
const newMovie = document.createElement("div");
newMovie.innerHTML = `
<div class="movie">
<img src=${movie.image}/>
<div>
<h3>${movie.name}</h3>
<span>Rating : ${movie.rating} / ${movie.date}</span>
<button id="deleteBtn">Delete</button>
</div>
</div>
`;
allMovies.append(newMovie);
const deleteBtn = document.querySelector('#deleteBtn');
deleteBtn.addEventListener("click", () => {
const exsistMovie = newMovie.remove();
movies.splice(exsistMovie, 1);
updateMovies();
});
but if i change deleteBtn
to newMovie
it will work, but i dont want to delete by clicking on movie, just delete when i click on this button..
i want to insert element to this div
<div class="movies"></div>
and its my all html code
<div class="header">
<h1 class="logo">My Project</h1>
<h5>Add Movie</h5>
</div>
<div class="formm">
<div class="form">
<div class="err">
<h1>Error</h1>
<p>You Entered Wrong Value..</p>
</div>
<h2>Add Your Movie</h2>
<input type="text" placeholder="Name Of Movie" id="movie" />
<input type="text" placeholder="image URL" id="image" />
<input type="number" placeholder="Rating" id="rating" />
<div class="btns">
<button id="submit">Submit</button>
<button id="cancel">Cancel</button>
</div>
</div>
</div>
<div class="movies"> </div>