Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event not working on dynamically created element

I'm pulling my hair out trying to figure out why the mouseover event won't work with the .on handler with a dynamically created element from ajax. The only thing that seems to work is the code with .live but I understand that it is deprecated.

$(".dropdown ul li").live("mouseover", function() {
alert('mouseover works');
});

However, when I try using .on, it will not work - no matter what variations I try (document.ready, .mouseover, etc etc)

$(".dropdown ul li").on("mouseover", function() {
alert('mouseover works');
});

The event handlers are at the bottom of the code, so they are executed last. Anyone have an idea of what I'm doing wrong??

like image 395
7ch5 Avatar asked Jul 02 '12 06:07

7ch5


People also ask

How do you add an event listener after an element was created dynamically?

To attach event handlers to the dynamically created button, we need to select the button with a class of btn and add an event listener of click . We're saying that onclick of the button, the p tag with a class of moreInfo should display block .

How can we call onclick function on dynamically created button in jQuery?

onclick = function () { alert("hi jaavscript"); };

How do you bind change event in jQuery for dynamically added HTML element?

If you try to bind the elements that are dynamically added to the DOM using the click() method of jQuery, this will not work, because it only binds the click event to elements that exist at the time of the “binding”. To bind the click event to all existing and future elements, use jQuery's on() method.

How is an event generated in jQuery?

jQuery makes it straightforward to set up event-driven responses on page elements. These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved.


1 Answers

Using .on for newly generated elements with dynamic event delegation http://api.jquery.com/on/ - where your main selector is an existent static parent:

$(".static-parent").on("event1 event2", ".dynamic-child", function() {

or in your case:

$(".dropdown").on("mouseover", "li", function() {
   alert('mouseover works!!!!!!!!!');
});

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Also make sure to use a DOM ready function

jQuery(function($) { // DOM is now ready and $ alias secured

    $(".dropdown").on("mouseover", "li", function() {
       alert('mouseover works!!!!!!!!!');
    });

});
like image 103
Roko C. Buljan Avatar answered Nov 15 '22 06:11

Roko C. Buljan