JQuery on () does not work with already added DOM elements

I have it:

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

What works when new elements are added to the DOM, but it does not 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 does not add new ones in the DOM. Is there a way for me to get this to work for both without me, to write this 2 times or call another function?

Here is a screenshot:

Shot

So here is a more detailed description of how this 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, click 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 calls the same one that was preloaded in php. The new one works (and any other additional ones are added) with the click event, and the first does not

@ArunPJohny

console.log($(".prev")); after adding a new item:

 [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] 
+6
source share
2 answers

The following is the content that I copied from the official jQuery website, it can explain this very clearly : Event handlers are bound only to the currently selected elements; they must exist on the page when your code makes a .on () call. . To ensure that elements are present and can be selected, bind events within the document, ready to be processed for elements located in the HTML markup on the page. If new HTML is injected into the page, select the elements and attach event handlers after the new HTML is placed on the page. Or use delegated events to attach an event handler, as described below.

Delegated events have the advantage that they can handle events from descendant elements that will be added to the document later. By choosing the item that is guaranteed to be present when the delegated event handler is attached, you can use delegated events to avoid having to frequently attach and remove event handlers. This element can be a view container element in the Model-View-Controller design, for example, or a document if the event handler wants to control all the bubble events in the document. The document element is available at the beginning of the document before loading any other HTML code, 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 children that are not yet created, another advantage of delegated events is their potential for much lower overhead when many elements need to be controlled. In a data table with 1000 rows in the body, this example attaches a handler to 1000 elements:

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

The approach with delegated events attaches the event handler to only one element, that is, and the event only needs to bubble at the same level (from clicking 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/

+2
source

No, if you are creating new elements like this, then you need to reinstall the event handler.

 function bindHandler() { $(document).on("click", ".prev", function(){ $(this).closest("div").find(".slider").animate({ "left": "+=450px" }, 800, "easeInOutBack"); return false; }); } bindHandler(); $(".add-another-pev").click(function(){ $.get("./data/get/PEVSection.php?pev="+pevGroup, function(data){ $("#simple-pevs").append(data); }); pevGroup++; bindHandler(); return false; }); 
-1
source

All Articles