I want to remove an element when I click the button and then add it again when I click the button again.
Don't get me wrong I don't want to "hide" the element I want to remove it.
I'm not really sure why this is not working but, my guess is that this creates a reference and once I delete the original element there would be nothing to add.
Is there any way to fix this using only native javascript?
function sortClick() {
var classes = document.getElementById('btn').classList;
var myNode = document.getElementById('myNode');
var clone = myNode.cloneNode(true);
if(!classes.contains('firstClick')){
classes.add("firstClick");
myNode.parentNode.removeChild(myNode);
}
else if(classes.contains('firstClick')) {
classes.remove('firstClick');
document.body.appendChild( clone );
}
};
#btn {
cursor: pointer;
}
.firstClick {
color: red;
}
<div id='btn' onClick='sortClick()'>click</div>
<div id=myNode>removed then added</div>