Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on() doesn't work on already added DOM items

I have this:

$(document).on("click", ".prev", function(){
    $(this).closest("div").find(".slider").animate({
        "left": "+=450px"
    }, 800, "easeInOutBack");
    return false;
});

Which works when new elements are added to the DOM, but it doesn't work for elements that are already in the DOM.

When I do this:

$(".prev").on("click", function(){
    $(this).closest("div").find(".slider").animate({
        "left": "+=450px"
    }, 800, "easeInOutBack");
    return false;
});

It works for elements in the DOM, but not new ones added to the DOM. Is there a way for me to get this to work for both without me having to write this 2 times, or make a call to another function?

Here is a screen shot:

Shot

So, Here is a more in depth description of how it works.

<div id="simple-pevs" style="padding-right: 20px;">
    <?php require_once __DIR__ . "/data/get/PEVSection.php"; ?>
</div>

Then When someone wants to add another section they click on the add button which then runs this code:

    $(".add-another-pev").click(function(){
        $.get("./data/get/PEVSection.php?pev="+pevGroup, function(data){
            $("#simple-pevs").append(data);
        });
        pevGroup++;
        return false;
    });

As you can see it is calling the same one that was pre-loaded in the php. The new one works (and any other additional ones added) with the click event and the fist one doesn't

@ArunPJohny

console.log($(".prev")); after a new element is added:

[a.prev, a.prev, prevObject: b.fn.b.init[1], context: document, selector: ".prev", jquery: "1.9.1", constructor: function…]
0: a.prev
1: a.prev
context: #document
length: 2
prevObject: b.fn.b.init[1]
selector: ".prev"
__proto__: Object[0]
like image 445
Get Off My Lawn Avatar asked Mar 08 '13 16:03

Get Off My Lawn


1 Answers

Below is the content that I copied from Jquery official website, It can explain it very clearly: 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.

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.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

$("#dataTable tbody tr").on("click", function(event){
alert($(this).text());
});

A delegated-events approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody):

$("#dataTable tbody").on("click", "tr", function(event){
alert($(this).text());
});

Note: Delegated events do not work for SVG.

http://api.jquery.com/on/

like image 143
OQJF Avatar answered Oct 12 '22 00:10

OQJF