Solved

A way to disable the Enter key in javascript?

Posted on 2012-04-06
2
393 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
Comment Utility
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
Comment Utility
Awesome.

Works perfectly.

Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now