4
I’m getting inside a div new elements via PHP using Xmlhttprequest.
So far so good, I get the elements and displays everything right, but I’m not getting the ID of these new elements using the event Click
.
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 do (but 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, only in Javascript(Pure) without using Jquery:
$('#res').on('click', 'li.new', function(){
alert( $(this).attr('id') ); //aqui retorna certo...
});
I Googled but I found nothing.
How could I do that?
If I understand correctly,
#res
is dynamically insightful– bfavaretto
@bfavaretto if #res is also dynamic then Event Handler can be added to the
window
: http://jsfiddle.net/9374tgbf/– Sergio
@Sergio worked out that’s just what I needed. Thank you
– Roma