alex89x
alex89x

Reputation: 479

How can I catch event on loaded pages with jQuery? ( on() not working)

I've a page in which I use the method Load() of jQuery to load some PHP code.

In my base page, I have the following jQuery code:

    $('.writeLink').on('click',function (e) {
    //alert("ok");
    var dataurl1 = $(this).attr("par1");
    var dataurl2 = $(this).attr("par2");
    if (dataurl1 != null)
    {
        $('#txtTo').val(dataurl1);
        $('#imgTo').attr('src',dataurl2);
        $('#writeModel').modal({onOpen: function (dialog) {
dialog.overlay.fadeIn('slow', function () {
    dialog.container.slideDown('slow', function () {
        dialog.data.fadeIn('slow');
    });
});

I have the #writemodel div on my base page.

The loaded PHP creates some divs with the .writeLink class, that should initiate the click event, but don't. I tried to use the .on() jQuery event but it doesn't work.

Any suggestions?

Upvotes: 1

Views: 97

Answers (1)

strah
strah

Reputation: 6722

Try this code:

$("#writemodel").on("click", ".writeLink", function(e){
    //do stuff
});

The click event is captured by the parent container, but the function executes only if the event originated in one of the '.writeLink' divs.

Upvotes: 6

Related Questions