Ben
Ben

Reputation: 702

Jquery .live() still doesn't run functions correctly

Though I have seen other posts similar, I haven't been able to find out the exact cause/solution to my problem, so I hope i'm not just reposting what others have asked:

I recently discovered that in jquery the .live() allows my functions to be attached to items which are dynamically added into the page. So I have a list of items, i click delete which uses the get function, using the href as a get variable, which then deletes that item from my database and refreshes the list.

So with the live function, I managed to then have this same function added to the refreshed list. The problem however is that the get variable specifiying which item to delete seems stuck on the last number.

So if I delete item 13, then the list refreshed, and I try to delete say item 15, it seems (from firebug) that the get variable is again attempting to delete item 13 and not 15

apologies for the messy code to follow:


    $(document).ready(function() {

    $('.delav').live('click', function(e) {
        e.preventDefault();
                var pager = $(this).attr("href");
        $("#dialog2").dialog({
            autoOpen: false,
            width: 600,
            modal:true,
          buttons : {
            "Confirm" : function() {
                  $('#ava_list').html("Loading..");

            var pai =$('#epa_id').val();
                $.get("/_includes/functions.php", { table: "availability", pa: pai,delattr: pager} ,function(data) {$('#ava_list').html(data);});
            $(this).dialog("close");
            },
            "Cancel" : function() {
              $(this).dialog("close");
            }
          }
        });

        $("#dialog2").dialog("open");

    });
    });

Upvotes: 1

Views: 208

Answers (2)

karim79
karim79

Reputation: 342635

It would appear that you have multiple input elements with the ID 'epa_id'. If that is the case, you will need to remove duplicate IDs and employ DOM traversal to grab the correct element relative to the clicked .delav instead.

For a specific solution, we will need to see your markup.

Upvotes: 3

secretformula
secretformula

Reputation: 6432

Rather than using $.live() the JQuery refrence recommends the use of delegates

    $('.highlevelclasshere').delegate('click', function(e) {
       e.preventDefault();
       var pager = $(this).attr("href");
        $("#dialog2").dialog({
           autoOpen: false,
           width: 600,
           modal:true,
        buttons : {
        "Confirm" : function() {
              $('#ava_list').html("Loading..");

        var pai =$('#epa_id').val();
            $.get("/_includes/functions.php", { table: "availability", pa: pai,delattr:         pager} ,function(data) {$('#ava_list').html(data);});
        $(this).dialog("close");
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog2").dialog("open");

});

Upvotes: 2

Related Questions