I have an unordered list with 2 li
, I must add more li
element with jquery also with a delete button, but now I need to remove this li
with the button.
The function works with the old li
but not with the new created li
...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Prueba front</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div id="text">
<span>Car: </span>
<input id="name" type="text"><button type="button" class="add">Add</button>
</div>
<div id="car-list">
<ul>
<li>
Ford <button type="button">Remove</button>
</li>
<li>
Nissan <button type="button">Remove</button>
</li>
</ul>
</div>
<script>
$(function ()
{
$('.add').on('click', function(){
var car=$("#name").val();
var li= $("<li> "+car +" <button type=button>Remove</button></li>");
$("ul").append(li);
});
$('button[class!="add"]').on('click', function(){
$(this).parent().remove();
alert("it works!!");
});
});
</script>
</body>
</html>
I need to remove the new li
created with jquery.
How can I achieve this?