2

I have a problem with my script.

In my table, every row has a url link which open modal window with email input to change this email without refresh page.

Script is ok if you change only one row. If I change second, third, etc row, every values changed by last one.

If I refresh page (F5) - script works perfectly.

Any helps ? Thanks a lot.

$(document).ready(function() {
  var table = $('#example').DataTable();
  
$('#example tbody').on('click', 'a', function () {
  var row = $(this).closest('tr[role="row"]');
  var data = table.row(row).data();
  
  $("#myModal").modal("show");
  $("#email").val(data[0]);
  
  $("#ulozit").click(function(){
    var novy_email = $("#email").val();
     $.ajax
     	   ({						
             method: "POST",
    	     url: "action_admin.php?action=uprava_emailu",
    	     data: { novy_email:novy_email, id:data[3] },
    	       success: function(data)
    		{
    		 table.ajax.reload();
    		 $("#myModal").modal("hide");							
    		}
    	    })		
    });
  });
});

1
  • your snippet doesn't work. import jQuery and post html Commented Aug 1, 2019 at 8:50

1 Answer 1

1

You have nested onClick handlers.

The second handler is added every time the first handler is called. The jQuery event model allows multiple handlers on one element, therefore a later handler does not override an older handler. The handlers will execute in the order in which they were bound.

In short: Do not nest your handlers.

Sign up to request clarification or add additional context in comments.

1 Comment

@Fajny_Kubo Nice, that worked better than I expected. The normal response here is always: And how do I do that? But you worked it out yourself. Bravo!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.