There are a couple of things wrong with your jsFiddle and your code.
Scope of the event handler
You are using an inline event handler. Inline event handlers are executed in global scope. However, because the fiddle by default wraps your JavaScript code inside a function, deleteEvent
is not global. The console shows the error:
Uncaught TypeError: Cannot read property 'appendChild' of null
There are three ways to solve this:
this
inside the event handler
Because you are calling the function as deleteEvent()
, this
won't refer to the DOM element that triggered the event. It will refer to window
, the global object, which doesn't have a parentNode
property.
If you use a different way to bind the handler, as suggested above, then that problem might solve itself. Otherwise, you have to call deleteEvent
in such a way that this
refers to the element:
onclick="deleteEvent.call(this)"
Of course you could also change the definition of deleteEvent
to accept the element as argument instead.
However, in your fiddle you actually attached the event handler to the <ul>
element, not to each <li>
element. In that case you have to pass the event object to function instead and get the original target from it:
onclick="deleteEvent(event)"
and
var deleteEvent = function (event) {
//Remove the parent list item from the ul
event.target.parentNode.removeChild(event);
}
DEMO
Wrong node reference
It seems you are deleting the wrong node in deleteEvent
. listItem
will actually be the <ul>
element, because that's the parent of the clicked element. Consequently, ul
will be the parent of the <ul>
element.
Since this
already refers to the <li>
element, there is need for listItem
here, and ul
should be this.parentNode
. In shorter form:
var deleteEvent = function () {
//Remove the parent list item from the ul
this.parentNode.removeChild(this);
}
return false
has no effect
return false
is often used to prevent the default action of the event. However, clicking on a li
element has no default action. Furthermore, the value has to be return from the event handler. deleteEvent
is not your event handler, it is called by your event handler. So if you want to return its return value, you have to do exactly that:
listItem.setAttribute("onclick", "return deleteEvent()");
But again, you should prefer a different of binding the event handler.
Invalid node creation
In the code in your question you are trying to call setAttribute
on an arbitrary object and try to append an arbitrary object to a DOM element. That won't work. You have to use document.createElement
to create a new element:
var li = document.createElement('li');
It seems you are doing this correctly in your fiddle.