简体   繁体   中英

jQuery bind event not firing on elements loaded via $().load()

I have a DIV that is in an .html file that is loaded into my document via:

$(document).Ready( function() {
    $("#contentDiv").load("some.html")


    //some.html contains a button id=saveButton
    $("#saveButton").click( function () {
        alert("Here I am!");
    }

});

The event will not fire. If I cut the content of some.html and put it in the document, uhm, "physically", the event will fire.

So, I am pretty sure this issue is related to the fact that the html is injected via .load().

It's bothersome, because if you look at the page source, all the HTML is in fact there, including the button.

So, the question is, is there ANY way to make this work? I am using .load() to reduce page complexity and increase readability, and, code-folding notwithstanding, I really do not want to have to pull all this HTML into the document.

EDIT : This code was just typed in off the cuff. It's not a cut-n-past of the actual code, and it is just to demonstrate what the problem is. But, thanks for pointing it out.

EDIT2 : Grrrrrrr. });

load() is asynchronus so you need to the job in the callback :

$(document).ready(function() {
    $("#contentDiv").load("some.html", function(){
        //some.html contains a button id=saveButton
        $("#saveButton").click( function () {
            alert("Here I am!");
        });
    });
});

Hope it helps :)

one way is by adding to the some.html the script line which will be loaded as the div appears.

You can add this script to some.html(in a script tag):

registerButton();

and then you can define registerButton() in your current document.

other way, if I remember correctly is by using something like the function bind( )

jquery load() function is asynchronous. If you want to bind events to the loaded content, you should put the code into the callback function:

$(document).ready(function() {
    $("#contentDiv").load("some.html", function() {
        //you should put here your event handler
    });

});

If you want to fire event on element which was not available at the time when DOM was ready then you need to use .on event.

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

$("#saveButton").on("click", function() {
      alert("Here I am!");
});

Your issue is that jquery load() function is asynchronous as @lucas mention. But his code has syntax errors, try this:

    $(document).ready(function () {
        $("#contentDiv").load("some.html", function () {
            $("#saveButton").click(function () {
                alert("Here I am!");
            });
        });
    });

Hope it helps now

You need to bind the event handler either after the load OR to the container of the HTML from the load

$(document).ready(function() {
  $("#contentDiv").load("some.html", function() {
    $("#saveButton").on('click',function() {
      alert("Here I am! Bound in callback");
    });
  });
});

OR use: (not needed that it be in the document ready just that the contentDiv be present)

$("#contentDiv").on('click','#saveButton',function(){
     alert("Here I am! bound to container div");
});

EDIT: load on the SAVE button click (per comments) (this makes no sense though)

$(document).ready(function() {
  $("#saveButton").on('click',function() {
    $("#contentDiv").load("some.html", function() {
      alert("Here I am! Bound in callback");
    });
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM