Via a click event, I injected an element with a custom attribute
<div class="added" myValue="hello"></div>
to the DOM.
I wrote a generic function:
$('.added').each(function(){
console.log( $(this).attr('myValue') );
});
Doesn't work. I thought $.each()
can handle dynamic elements?
UPDATE to the question:
I realize using .each()
is the wrong approach. But what to do if I don't want to attach an event handler to the element. I just want to automatically do stuff to any elements of a certain class name, regardless of when they've been added to the DOM. Seems like a common use case.