Created
June 13, 2014 16:03
-
-
Save anthonycrumley/f330e5f13c874a4cae3b to your computer and use it in GitHub Desktop.
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
// We are doing something like this in several places | |
$("#student_id").bind("keypress", function(e){ | |
if (e.keyCode == 13){ | |
jQuery("#student_submit").parent().parent('form')[0].submit(); | |
return false; | |
} | |
}); | |
// The following are a couple of approaches to encapsulate this. | |
// Add an event to all text fields in the app that can be listened for. | |
// Following would replace what we are doing now | |
$('#student_id').bind("enterKey",function(e){ | |
jQuery("#student_submit").parent().parent('form')[0].submit(); | |
}); | |
// The following would be in something like application.js | |
$(':text').keyup(function(e){ | |
if(e.keyCode == 13) | |
{ | |
$(this).trigger("enterKey"); | |
} | |
}); | |
// Add a jQuey method that can be called on specific text field | |
// The following would replace what we are currently doing. | |
$('#student_id').onEnter(function(){ | |
jQuery("#student_submit").parent().parent('form')[0].submit(); | |
}) | |
// The following would be somewhere like application.js | |
$.fn.onEnter = function(fn) { | |
return this.each(function() { | |
$(this).bind('enterPress', fn); | |
$(this).keyup(function(e){ | |
if(e.keyCode == 13) | |
{ | |
$(this).trigger("enterPress"); | |
} | |
}) | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment