Solved

A way to disable the Enter key in javascript?

Posted on 2012-04-06
2
397 Views
Last Modified: 2012-04-06
Hi,

Is there a way to disable the Enter key in javascript?

I would be OK if it behaved as a Tab key.

Thanks
0
Comment
Question by:taborrg
2 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37818131
There you go.
Both solutions. The top form disables enter/return keypresses. The bottom form turns enter/return into TAB (functionally, as in actuality it kills the enter/return, but sends the focus to the next input field).
Notice that you have to be careful not to mix the ids of the input fields.

<html>
<head>
<script type="text/javascript">
	// this will kill the enter/return keys
    function censorship(e){
        var keycode=e.charCode? e.charCode : e.keyCode;
        if (keycode==10 || keycode==13){ // check both RETURN and ENTER
            return false;                // disable keypress
        }
    }
    // this will try to focus on the next input field
    function tabularasa(e,f){
        var keycode=e.charCode? e.charCode : e.keyCode;
        if (type='keypress' && (keycode==10 || keycode==13)){  // check both RETURN and ENTER
            document.getElementById(f).focus();                // move to the specified field
            return false;
        }
    }
</script>
</head>
<body>
<form name="forme1" method="get" action="">
    <label for="field1">Input field 1: <input type="text" name="field1" id="field1" onkeypress="return censorship(event);" ></label><br>
    <label for="field2">Input field 2: <input type="text" name="field2" id="field2" onkeypress="return censorship(event);" ></label><br>
    <label for="field3">Input field 3: <input type="text" name="field3" id="field3" onkeypress="return censorship(event);" ></label><br>
    <input type="submit">
</form>
<hr>
<form name="forme2" method="get" action="">
    <label for="field4">Input field 4: <input type="text" name="field4" id="field4" onkeypress="return tabularasa(event,'field5');" ></label><br>
    <label for="field5">Input field 5: <input type="text" name="field5" id="field5" onkeypress="return tabularasa(event,'field6');" ></label><br>
    <label for="field6">Input field 6: <input type="text" name="field6" id="field6" onkeypress="return tabularasa(event,'field4');" ></label><br>
    <input type="submit">
</form>
</body>

Open in new window

0
 
LVL 1

Author Closing Comment

by:taborrg
ID: 37818154
Awesome.

Works perfectly.

Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question