javascript - Controlling ENTER key -


how can make form in page doesn't submit on pressing enter — rather. same work pressing particular button or icon or image?

here contents of form:

<input type="text" id="txt" /><input type="button" value="search"              onclick="searchresult()" /> 

the problem if press enter, form submits , text field clears function searchresult() doesn't show effect. when pressing button, works well.

html

<input type="text" id="txt"/> <input type="button" value="search"/> 

jquery

$('input[type=text]').on('keyup', function(e) {   if(e.which == 13) {   // 13 keycode enter     e.preventdefault();   } }) 

you can bind submit() following

$('form').submit(function(e) {  // instead of `form`,                                  // use `id` or `class` combination    if(e.which == 13) {      e.preventdefault();    } }); 

remainder

don't forget place code within

$(document).ready(function() {    // code }); 

in short

$(function() {   // code }); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -