?
Solved

A way to disable the Enter key in javascript?

Posted on 2012-04-06
2
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Suggested Courses

800 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