Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHPWind-- A PHP forum script applcaition in China
- How to blend adsense inside your post?
- Formatting my post
- Notepad++ - Add C++ compiler
- Install PHPMailer 5.2.4 and use smtp gmail
- Set up a child account and set screen time limit in Windows 8
- Wayback Machine - see archived versions of web pages across time
- phpexcel toggle expand and hide column in EXCEL and summary
- Install PHP ibm_db2 extension in Linux (redHat)
- PHP: add a download as pdf file button in report page
Wednesday, January 16, 2013
onkeypress and onkeydown, prevent enter key to submit a form in input
In JavaScript, the keypress event fires after the key is pressed and released, while keydown fires after the press but before the release.
Example:
<script type="text/javascript">
function handleKeyPress(e,form){
var key=e.keyCode || e.which;
if (key==13){
e.preventDefault();
}
}
</script>
<button type='button' id="filter" onclick="searchstudent()"> Serach </button>
<input type="text" id="search" maxlength="80" onkeypress="handleKeyPress(event,this.form)" onkeyup="searchstudent()" >
In this example, when any key is pressed, Javascript function searchstudent() is called. We do not have to wait all the text typed to start search. Somehow it is similar to Ajax.
To prevent enter key or return key (key code 13) submit the form, we defined a user function handleKeyPress in onkeypress, which called e.preventDefault(); to prevent form submission.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment