I have a button that creates paragraphs dynamically in a div along with an ID.
When you create a paragraph you can also select that paragraph (“background color changes”) by clicking it, along with that (“When you click on paragraph”) a div is created with a link inside of it.
Tried to remove individually but it would remove all the paragraphs at once
How can I remove a paragraph that is being selected(“I mean individually”)?
Here is link and what I have so far:
Javascript/jQuery Code:
$(document).ready(function(){
var id = 1;
$("#push").on({
click: function(){
var pr = $('<p class="test">This is text ' + id + '</p>');
var d = $("#Test");
var pclone = $(pr).clone();
pclone.on({
mouseenter: function(){
$(this).addClass("inside");
},
mouseleave: function(){
$(this).removeClass("inside");
},
});
pclone.appendTo(d);
id++;
}
});
var div = $('<div class="customD" id="d"></div>');
var del = $('<a href="#" class="delete" id="erase">Delete</a>');
var pcust = $(div).clone();
var pdel = $(del).clone();
$("#Test").on("click", "p", function(){
var cur = $(this).css("background-color");
if(cur=="rgb(255, 255, 255)") {
$(this).css("background-color","red").addClass("help insider");
$(this).after(pcust);
}
else {
$(this).css("background-color","white").removeClass('help insider');
$(pcust).remove();
}
$(pcust).append(pdel);
});
});
HTML Code:
<html>
<body>
<a href="#" id="push">Push</a>
<div id="Test"></div>
</body>
</html>
CSS Code:
.test { color: #000; padding: .5em; margin: 0px; border: 2px solid white; background: white;}
.help { border: 2px dashed #FDD }
.inside { border: 2px solid red }
.insider { border: 2px solid #FDD; }
.delete { font-size: 12px; }
.customD { background: yellow}
You can add click event handler to delete link
BTW. You don’t have to clone each time link and its parent div, you can build up that part of dome and hold same reference, if you reuse it with