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
- Using Cron Job to process PHP scripts
- job interview questions (1)
- PHP connect IBM db2 database in XAMPP
- Datatable export excel wraptext and newline
- Install PHPMailer 5.2.4 and use smtp gmail
- Set up a child account and set screen time limit in Windows 8
- Free host forum is not reliable
- PHP - Export Content to MS Word document
- Powerful js library datable to export table in pdf, excel, sorting, freezing column and table header fixed (sticky)
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