Created
January 24, 2014 20:42
-
-
Save clarkf/8605959 to your computer and use it in GitHub Desktop.
jQuery Helper for data-method functionality
This file contains 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
jQuery(function ($) { | |
/** | |
* Handle clicks on links with the "data-method" attribute, such as | |
* | |
* <a href="/resources/1" data-method="delete">Delete Resource</a> | |
*/ | |
$(document).on('click', '[data-method]', function () { | |
var $this = $(this), | |
method = $this.data('method'), | |
action = $this.attr('href'), | |
form = $('<form />'), | |
input = $('<input />'); | |
form.attr('action', action) | |
.attr('method', 'post'); | |
input.attr('name', '_method') | |
.attr('type', 'hidden') | |
.attr('value', method) | |
form.hide() | |
.append(input) | |
.appendTo($('body')); | |
form.submit(); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment