dimanche 3 décembre 2017

how to generate dynamic id using variable and delete the element using the same variable

Hi i am creating a small web application where if i click create element it should create new button using variable x. When i click the remove element it should delete from the last element until no new element should be there.

But the problem is when i click create element i am not able to add id to the newly created element dynamically.At the same time how can i delete using variable as id to delete new element.

Here element means Button

<!DOCTYPE html>
<html>
<head>
<script 
src="http://ift.tt/2nYZfvi">
</script>
<script>
var x=1;
var fd,d;
$(document).ready(function(){

$("#r").click(function(){
$("#fd").remove();

});

$("#c").click(function(){

var txt="<div id='fd"+x+"'><button > New Button </button></div>";
x++;
$("#d").after(txt);

});
});
</script>
</head>
<body>
<button id="c">Create Element</button>
<button id="r">Remove Element</button>
<br> <br>
<div id="fd">
<button id="d">First Element</button>
</div>
<br>
</body>
</html>

I have included the image where i am able to create new element but not able to delete it

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire