Created
July 22, 2008 02:18
-
-
Save jlindley/608 to your computer and use it in GitHub Desktop.
Merb/jQuery: add delete method to destroy links.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make merb resource route url link_to's with class deletable fake delete HTTP method. | |
jQuery.fn.deletable = function() { | |
return this.each(function() { | |
var delete_link = $(this); | |
var hidden_form = document.createElement('form'); | |
hidden_form.style.display = 'none'; | |
delete_link.append(hidden_form); | |
hidden_form.method = 'POST'; | |
hidden_form.action = delete_link.attr('href'); | |
var method_input = document.createElement('input'); | |
method_input.setAttribute('type', 'hidden'); | |
method_input.setAttribute('name', '_method'); | |
method_input.setAttribute('value', 'delete'); | |
hidden_form.appendChild(method_input); | |
this.form = hidden_form; | |
delete_link.click(function() { | |
if (confirm("Are you sure?")){ | |
this.form.submit(); | |
} | |
return false; | |
}); | |
}); | |
}; | |
$(document).ready(function() { | |
$('a.deletable').deletable(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment