Created
January 3, 2016 22:46
-
-
Save nijjwal/aac0e7d806fda323e393 to your computer and use it in GitHub Desktop.
Disable enter key in JavaScript
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Disable Key in JavaScript</title> | |
<script> | |
function noenter(e) { | |
//1. window is an object that represents an open window in a browser. | |
//2. event is an action that can be detected by javascript. | |
//Sometimes we want to execute a JavaScript when an event | |
//occurs, such as when a user clicks a button. | |
//3. Internet Explorer doesn't pass event to handler. | |
//Instead you can use window.event object which is being | |
//updated immediately after the event was fired. | |
//So the crossbrowser way to handle events: | |
//4. window is an object, event is a property that contains the | |
//last event that took place. | |
//5. The followig code means e argument is optional. So if you call the method | |
//with no arguments it will use a default value of "window.event" | |
e = e || window.event; | |
var key = e.keyCode || e.charCode; | |
//alert('e.type: ' + e.type + '; key: ' + key); | |
return key !== 13; | |
} | |
</script> | |
<style> | |
textarea { width:300px; height:200px; } | |
</style> | |
</head> | |
<body> | |
<form method="POST" action="google.com"> | |
<textarea>txtbox1</textarea> | |
<textarea onkeypress="return noenter(event)">txtbox2</textarea> | |
<input type="submit" value="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brilliant!! So simple and super effective. I have 3 text fields that need input from a barcode reader.
Once the barcode reader gets the data it adds a cr and of course would submit the incomplete form.
This work perfectly (and i'm very new to Javascript) had it installed in 2 minutes thanks so much!!