Skip to content Skip to sidebar Skip to footer

Jquery Ajax Is Giving The Same Result To All Divs

I am trying to be able to rename files. At the moments its working but, for example lets say i have: file one name is 'hello', file two name is 'bye', file three name is 'hey' and

Solution 1:

I believe the problem is that each time you rename a file, you add a new click listener to document, but you never clean it up. When you rename the next one it actually calls both rename functions. You could save the name of the file to be renamed in a variable, and then in a single click listener set on page load rename the file. Also you'd have to have a way to tell if the renaming has already occurred so that it won't be called on every page click.

Solution 2:

You have to wrap out your inner click binding, so you'd have:

$('.edit').click(function(e){
    e.preventDefault();
    var auth = $(this).attr('id');
    var each = $(this).closest('.each_file').find('.fnl');

    $.post( "ajax/edit_filename.php", { auth:auth })
       .done(function( data ) {

        $( "#dialog" ).dialog({
          modal: true,
          resizable: false,
          title: 'Edit file name',
          buttons: {
              "Close": function() {
                  $(this).dialog("destroy");
                  $(this).dialog("close");
              }
          } 
        });
        $('.ui-dialog-content').html('<input type="text" class="newfname" value="'+data+'"/><div class="btn_l"><input type="submit" class="submit_btn" id="edit_filenameb" value="Edit Name" /></div>');
    });

    //Code from here took to outside
});

//This was taken out from the other click binding
$(document).on('click', '#edit_filenameb', function(e){
    e.preventDefault();
    var nname = $('.newfname').val();
    var auth = ... //Get the auth hereconsole.log(nname);

    if(nname == ''){
        $('.submit_btn').effect('shake');
    } else {
        $.post('ajax/change_filename.php', {nname:nname, auth:auth})
        .done(function(data){
            each.text(data);
            $('#dialog').dialog('close');
        });
    }
});

Hope this helps. Cheers

Post a Comment for "Jquery Ajax Is Giving The Same Result To All Divs"