I'm recently learning JavaScript and don't understand some things yet. In this example, I want a button with two event listeners, but not executed at the same time. That's it:
The web page has two elements: One paragraph(<p>
) and one button. This button should remove the <p>
element and restore it too on alternating clicks.
1.- Add an event listener to the button (remove).
|--- Remove the <p> element.
|--- Remove the <p> element
|--- Remove the listener and add a new event listener (restore).
2.- With the new event listener (restore), the button restore the <p> element.
|--- Remove the event listener (restore)
|--- Add a new event listener (remove);
3.- Back to 1.
I can assign the event listener to the button, but I can't remove it. Once the button has the remove event, this event is never removed and the new event is never added. Why does this happen? If I'm wrong, please tell me better code practices.
Here is the code in jsfiddle: http://goo.gl/4CX55g
Thank you very much.