I'm getting inside a div new elements via PHP using XMLHttpRequest .
So far so good, I get the elements and it displays everything right, but I'm not able to get the ID of these new elements using the Click
event.
Ex:
<div id="res">
<li class="new" id="50">Novo Elemento</li>
<!-- aqui é inserido o novo elemento via XMLHttpRequest (do PHP). -->
</div>
In my JS I do (but it does not work):
var el = document.getElementsByClassName('new');
for(var i = 0; i < el.length; i++){
el[i].addEventListener('click', function(){
alert( this.getAttribute('id') ); //aqui seria o ID que to precisando. :(
}, false);
}
I need something similar to the code below, but in JavaScript (Pure) without using JQuery:
$('#res').on('click', 'li.new', function(){
alert( $(this).attr('id') ); //aqui retorna certo...
});
I searched for Google but found nothing.
How could I do this?