I’m trying to find out if its possible to create a class clicklistener. When i create a div listener, i can first add the listener and afterwards i can create the div.
So i want to create the listener first and later i add the class. Is it possible?
html:
<div>
<div id="1">
<div id="2">
<!-- will be created.. -->
</div>
</div>
</div>
js:
$(".welcome").click(function() {
alert("Hi there");
});
// Later the class album will be loaded
// When you put it above it will work..
welcome = document.getElementById("2");
welcome.innerHTML = '<div id="3" class="welcome"> Hello</div>';
I made a jsfiddle to test.
yes…use on event delegate…
try this
even more better is if u attached the event to closest element…in your case that is div with id #2
<div id=2>fiddle here