Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Restful DELETE request using <a>
$(document).ready(function() {
// Add click handler to hyperlinks to send restful DELETE requests
//
// Example:
//
// <a href="/delete/1" class="rest-delete">delete</a>
// <script>restful.init($('.rest-delete'));</script>
//
var restful = {
// TODO: add various configurations, e.g.
// - do_confirm: [ true | false ]
// - confirm_message: "Are you sure?"
// - do_remove_parent: [ true | false ]
// - parent_selector: '.li' '.div' ...
// - success: (closure)
init: function(elem) {
elem.on('click', function(e) {
self=$(this);
e.preventDefault();
if(confirm('Are you sure?')) {
$.ajax({
url: self.attr('href'),
method: 'DELETE',
success: function(data) {
self.parent('li').remove(); // todo: make configurable
},
error: function(data) {
alert("Error while deleting.");
console.log(data);
}
});
};
})
},
}
restful.init($('.rest-delete'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.