I'm kinda new to javascript, and probably there are other people who already asked similar question, but I hope you can help me anyway. I'm trying a simple basic operation of add/remove of a div. The add works fine, the remove is never called.
function $(el) {
return document.getElementById(el);
}
function remove() {
console.log("remove called");
var child = $('second');
}
function addContainer() {
console.log("addContainer called");
var aContainer = document.createElement('div');
aContainer.setAttribute('id', 'second');
aContainer.innerHTML = "<a href=\"#\" onclick=\"remove()\">second</a>";
document.body.appendChild(aContainer);
}
In the addContainer I try to add the onclick function callback, but apparently it doesn't work.
Here the jsfiddle of reference http://jsfiddle.net/m8kyav2b/
DO you know why 1- the remove function is never called? 2- once I click on the remove link, the innerHTML is removed, but not the div "second"?
Thanks a lot in advance