Skip to content

Instantly share code, notes, and snippets.

@austra
Created March 9, 2015 16:33
Show Gist options
  • Save austra/51191fe7fd043ae31ae8 to your computer and use it in GitHub Desktop.
Save austra/51191fe7fd043ae31ae8 to your computer and use it in GitHub Desktop.
rails jquery unobtrusive onclick
Obtrusive:
link_to 'Another link with obtrusive JavaScript', '#',
:onclick => 'alert("Please no!")'
Unobtrusive:
rails:
link_to 'Link with unobtrusive JavaScript',
'/actual/url/in/case/javascript/is/broken',
:id => 'my-link'
jQuery:
$('#my-link').click(function (event) {
alert('Hooray!');
event.preventDefault(); // Prevent link from following its href
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment