Skip to content

Instantly share code, notes, and snippets.

@clarkf
Created January 24, 2014 20:42
Show Gist options
  • Save clarkf/8605959 to your computer and use it in GitHub Desktop.
Save clarkf/8605959 to your computer and use it in GitHub Desktop.
jQuery Helper for data-method functionality
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