I would like add an event such as onclick or mouseover to a dynamically created element (similar to the .live function in jQuery)...how do I do this using pure javascript without a framework such as jQuery?
document.getElementById('create').onclick = function(){
var newdiv = document.createElement('div');
newdiv.addClass('new');
document.body.appendChild(newdiv);
};
document.getElementsByClassName('newdiv').onclick = function(){
alert('Success');
};
#create {
width:150px;
height:25px;
margin-bottom:5px;
background:#ccc;
border:1px solid #222;
}
.new {
width:200px;
height:100px;
background:#ffccff;
border:1px solid #333;
}
<html>
<body>
<div id="create">
Click to create div
</div>
</body>
</html>
I would like to be able to do this from the newly created divs class instead of an id.
Any help would be greatly appreciated