Remove self element onclick
You have to pass this
through the function call in html onclick
so it could refer to the element then you have to use it as parameter in the function definition, otherwise, if you use this
in the function definition, it will just refer to the window object.
This will work fine
function remove(el) {
var element = el;
element.remove();
}
<div id="i" onclick="remove(this)">Sample text</div>
your remove function should be like this
function remove(elem){
elem.parentNode.removeChild(elem);
}
your are passing a this
in your html, which is the html tag itself, however, when you using this
in your js function, that this
is the function itself, so you will get error by trying to use the js function's id as element id
Minimalist solution:
<div onclick="this.remove()">Sample text</div>