I have this code
$(".myclass").on("click", function(e){
alert('Hello');
});
Now its not working on dynamicaly added elements
How can i use like live function
To use it like live you have to use "delegation", meaning you attach the handler to a parent element that is present from start, like:
$(document).on('click', '.myclass', function(e){
alert('Hello');
});
Note what it is saying in the docs:
Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.
Also, in case you are worried about performance issues, try to attach the handler as "low" as possible:
Attaching many delegated event handlers near the top of the document tree can degrade performance. Each time the event occurs, jQuery must compare all selectors of all attached events of that type to every element in the path from the event target up to the top of the document. For best performance, attach delegated events at a document location as close as possible to the target elements. Avoid excessive use of document or document.body for delegated events on large documents.