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
- PHP: add a download as pdf file button in report page
- How to blend adsense inside your post?
- Formatting my post
- PHPWind-- A PHP forum script applcaition in China
- Notepad++ - Add C++ compiler
- PHP connect IBM db2 database in XAMPP
- Datatable export excel wraptext and newline
- phpexcel toggle expand and hide column in EXCEL and summary
- Sweet Alert JS library - beautiful replacement of JavaScript Alert
- ActionScript 3.0 demo: create a falling snow in flash CS6
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