Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created March 28, 2011 02:20
Show Gist options
  • Save susanBuck/889890 to your computer and use it in GitHub Desktop.
Save susanBuck/889890 to your computer and use it in GitHub Desktop.
checkEnter
<a href='/classes/viewSource/?path=<?=$_SERVER['PHP_SELF']?>' target='_blank'>View Source</a><br/><br/><br/>
/*-------------------------------------------------------------------------------------------------
Check enter
-------------------------------------------------------------------------------------------------*/
function checkEnter(e,submitFunction){
// Put this in the last field of a form like this : <input type="text" onKeyPress="checkEnter(event,hey)">
// e is the event.. its passed in just as event... It looks like this : keyup charCode=0, keyCode=13
// submit function is the function we'll call, pass it in as a string!
// if params are needed for the submitFunction they get passed in too - as many as are needed. we'll invoke arguments to fetch all of them.
// example - function with no paramters : onKeyPress='checkEnter(event,\"checkZip\")'
// example - function with paramaters : onkeyup='checkEnter(event,\"changeQuantity\",$optionId,this.value,\"cartText\")' optionId,this.value,cartText are the paramaters to changeQuantity
// Build our paramaters for the submitFunction (if there are any)
var theseParams = "";
// Go through all the params after the first 2 (first two are e and submitFunction)
for (var i = 2; i < checkEnter.arguments.length; i++) {
theseParams += "'" + checkEnter.arguments[i] + "'";
// If this is the last one, don't add the comma at the end
if(i != checkEnter.arguments.length - 1) {
theseParams += ",";
}
}
try{ // If i don't use this try statement, it'll throw an error when tab is pushed
var characterCode;
if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}
if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
// Run our submit function with any paramaters
eval(submitFunction + '('+theseParams+')');
return false;
}
else{
return true;
}
}
catch(err) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment