Reputation: 475
I am unable to use click() event on a button which is loaded through
html().
$.post('includes/addconnectordetails.php',{connnumb: connno},function(data){
$('#divgeneratecheck').html(data);
});
I am loading a form from addconnectordetails.php page on div#divgeneratecheck, form is loading but i am not able to use .click() event on the button displayed in the form. How can I do that ?
Upvotes: 1
Views: 821
Reputation: 8767
The issue that you are experiencing is due to event delegation. When you are trying to attach the event listener via the click() event handler, it will only reference elements that were created initially, not dynamically loaded elements.
Think of creating a map and then trying to use that map for a location that was not on it previously. Since it was initially absent, you would not know it exists and as such are unable to normally reference it. However, delegated events essentially allow that location to be added to the map after it was created and then be referenced even though it wasn't initially there.
The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live(). To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()
In addition:
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.
Upvotes: 3
Reputation: 1345
You need to use .on
, you have to attach it to a parent of the items you want to have the event. I use body, but in your case could be #divgeneratecheck
$('body').on('click','SELECTOR',function() {
/* Code here */
});
Just replace selector with the parts of the dom you want to attach the event to. (like #btn1 or .button)
Upvotes: 4
Reputation: 2815
jquery cannot hook dynamically loaded html without some help, take a look at live(), on() or delegate() within the jquery website.
Upvotes: 1
Reputation: 9080
You will have to use on()
for dynamic elements:
$('selector').on('click', function() {
// your code
});
Upvotes: 4