Crazy Horse
asked on
How to refactor edit/delete jQuery to include else statements
I had this question after viewing confirm popup before record delete takes place.
I am trying to make this code shorter by possibly changing to say if I click on edit then run the edit ajax otherwise if I click on delete then run the delete ajax.
I am trying to make this code shorter by possibly changing to say if I click on edit then run the edit ajax otherwise if I click on delete then run the delete ajax.
$(document).ready(function() {
$('.delete').on('click', function (e) {
e.preventDefault();
var $locationId = $(this).data('location-id');
var $locationName = $(this).data('location-name');
var $action = $(this).data('action');
console.log($action);
console.log($locationId);
console.log($locationName);
swal({
title: 'Are you sure?',
text: "You won't be able to undo this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#FB404B',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}, function (isConfirm) {
if (isConfirm) {
$.ajax({
type: 'post',
url: 'formhandler.php',
data: {locationName: $locationName, locationId: $locationId, action: $action},
success: function (result) {
window.location.href = 'locations.php';
},
});
}else{
console.log('cancelled');
}
});
});
$('.edit').on('click', function (e) {
e.preventDefault();
var $locationId = $(this).data('location-id');
var $locationName = $(this).data('location-name');
var $newLocationName = $(this).parents('tr').find("input[type='category']").val();
var $action = $(this).data('action');
console.log($action);
console.log($locationId);
console.log($locationName);
console.log($newLocationName);
$.ajax({
type: 'post',
url: 'formhandler.php',
data: {locationName: $newLocationName, locationId: $locationId, action: $action},
success: function (result) {
window.location.href = 'locations.php';
// alert(result);
},
});
});
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I am saying - keep the functions separate and if there is overlap (and it makes sense to do so) move that functionality into a function with parameters.
ASKER
So, are you saying it would be better to keep the code as per my original post?