Right now, my code allows someone to create an li by submitting a movie title and its rating. A remove button also appears at the end of this li. I want this li to be removed once the remove button is clicked on. I understand how to do this with Javascript, however how do you do this with jQuery? The ways I have attempted this are:
$('li button').click( function(e) {
e.target.closest('.li').remove();
})
$('li').on("click", "button", function() {
$(this).remove()
})
I know there are other ways in Jquery to remove the li, but what is wrong with the above two methods? is it how I selected the items? My entire HTML and JS are below. Thanks in advance!
$('#movieForm').submit(function(event) {
event.preventDefault();
let movieInput = $("#movieTitle").val();
let ratingInput = $("#rating").val();
$('#movieList').append("<li>" + movieInput + " " + ratingInput + "<button>Remove</button></li>");
})
$('li button').click( function(e) {
e.target.closest('.li').remove();
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Movies App!</title>
<link rel="stylesheet"
href="https://unpkg.com/bootstrap/dist/css/bootstrap.css">
</head>
<body>
<h1>Movies App!</h1>
<ul id="movieList">
</ul>
<form id="movieForm">
<input id="movieTitle" placeholder="Movie Title">
<input id="rating" placeholder="Rating">
<input type="submit" id="submit"></button>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="MoviesApp.js"></script>
</body>
</html>