Stopping the onkeypress event after altering focus

I have a form where my users are asking for the ability to move focus to the next field when the enter key is pressed, instead of submitting the form. To achieve this, I have implemented an onkeypress event handler on the text input field to change the focus when the enter key is pressed.

The issue I am facing is that even though the focus changes as expected, the form still gets submitted as if the enter key was pressed again. I tried returning false in the event handler function to prevent this behavior, but it doesn't seem to work. This problem occurs in both Chrome and Firefox.

I would appreciate any insights into what might be causing this unexpected behavior.

<form action="" name="confirmation" method="post">
    <fieldset>
        <div class="clearfix">
            <label for="id_event_fuel_amount">Quantity:</label>

            <div class="input">
                <input type="text" id="event_fuel_amount_id" name="event_fuel_amount" onkeypress="keydown(event, &#39;event_purchase_amount_id&#39;)" />
                
            </div>
        </div>
        <div class="clearfix">
            <label for="id_event_purchase_amount">Sale:</label>

            <div class="input">
                <input type="text" id="event_purchase_amount_id" name="event_purchase_amount" />
                
            </div>
        </div>

        
        <input type="hidden" name="state" value="3" id="id_state" />
        
        <input type="hidden" name="time_stamp" value="2011-09-24 14:34:06" id="id_time_stamp" />
        
        <input type="hidden" name="purchase" value="66" id="id_purchase" />
        

        <input type="submit" value="Save"/>
    </fieldset>
</form>

<script>
    function keydown(e,s){
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        if (code==13){
            document.getElementById(s).focus();
            return false;
        }
    }
</script>

Answer №1

Experiment with

e.preventDefault();

This method is effective in stopping the submission of the form event.

Answer №2

This solution could be useful:

<script type="text/javascript">
document.getElementById('world').onkeydown=function(e){
    var e=window.event || e;
    if (e.keyCode == 13) return false;
}
</script>
<input type="text" id="world" />

Check out this example: JavaScript demo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Uses of SystemJS with TypeScript Loader

Can someone help clarify something about this TypeScript plugin for SystemJS? https://github.com/frankwallis/plugin-typescript/ Here is what the plugin does: This SystemJS plugin allows you to import TypeScript files directly and have them compiled in ...

I am unable to access the "image" field on the "Query" type in GraphCMS when using Gatsbyjs

Hello there, I'm completely new to Gatsby and Graphql. I've been diving into the CMS system but keep encountering this error that I can't quite figure out. Any guidance would be greatly appreciated. Below is the exact error message I've ...

What is the method to generate an array of values using a single attribute in GeoJSON data?

Note: After reviewing some possible solutions mentioned in this thread, I found that .map is the perfect fit for what I need, which was not covered in the original post. Thomas's response below addresses my specific requirement. In JavaScript, how ca ...

Utilizing regex patterns within Jqgrid

I am currently utilizing the JqGrid plugin (version 4.6.0 - jQuery Grid) to filter my results using specific fields. Here is an example of how the GUI appears: https://i.sstatic.net/BqGai.png With a large list of employees, I want the ability to filter t ...

Vue.js live timestamp update

I have a function that returns the current timestamp with date and time, but it remains static upon page load without updating (meaning: the seconds and minutes do not change in real-time). Desired Outcome: I want the time to be continuously moving and no ...

Switch between using a dropdownlist and textbox depending on the selection in another dropdownlist

In the process of developing an application, I have implemented a country dropdown list and a state dropdown list. Here's the scenario: when a user selects a country, the states for that country will populate in the state dropdown list. However, the ...

How to deselect a checkbox in next.js when another one is selected

I'm looking to create a navigation system where clicking on a button scrolls to a specific section. However, I'm wondering how to deselect three inputs when one is selected in next.js using the code below. Do I need to modify each menu item indiv ...

Loading the Facebook JavaScript SDK asynchronously using React and Redux

I have a process that involves loading a JavaScript SDK, which looks like this: class Facebook{ constructor(){ window.fbAsyncInit = () => { FB.init({ appId : 'myappID', ...

Implement a mouseenter event to all input elements that have specific names stored in an array, utilizing jQuery

I'm struggling to figure out how to apply my function to all input elements with a name that is included in my array. This is what I've attempted so far: var names = ["name1", "name2"]; $(document).ready(function(){ $('input[name=names[ ...

Turn off integrity verification for local dependencies in package-lock.json

Is there a way to bypass the integrity check for a local dependency in package-lock.json? Within my project repository, I have a core library along with two Angular applications that both rely on this core library as a dependency. The problem arises beca ...

Is it possible to add some color to the first table in the div

Is it possible to change the background color of the first table within this Div tag that contains three tables? <div id="WebPartWPQ2" width="100%" HasPers="false" allowExport="false"> <table width="100%" border="0" cellSpacing="0" cellPadding= ...

NodeJS is throwing a `ReferenceError` because the `io` variable is not

I am working on a NodeJS project and I need to access a variable that is defined in my app.js file from another file. Is this possible? Here is my code: app.js var app = express(); var io = require('socket.io').listen(app); ... otherFile ...

"Utilizing Ramda's map function to integrate dynamic keys: A step-by-step guide

I am currently working with an array structured like this : array = ['2020-06-03', '2020-06-05', '2020-06-06'] My task is to transform it into the following format : Object { "2020-06-03": Object { "selec ...

Handlers for mouseover, mouseout, and click events are failing to trigger

Take a look at this code snippet - setEvents:function(a, b){ if(b) { $('#id').mouseover(function(){ console.log('mouse over') }) $('#id').mouseout(function(){ console.l ...

JavaScript forEach functionality is not compatible with Dynamics CRM 2016

I'm currently working on writing some JavaScript code for a ribbon button in Dynamics CRM 2016 that will extract phone numbers from a list of Leads displayed in the Active Leads window. However, I've encountered an error message when attempting ...

Using getStaticProps in Next.js to retrieve menu data and seamlessly integrate it into the layout

I have integrated Sanity.io as a backend for my project, specifically managing the data for my main menu in parent/children object arrays. While I am able to successfully fetch this data, I prefer to utilize getStaticProps to ensure that my site remains c ...

Keep the footer at the bottom of the screen without needing to define a 100vh in Material UI

In the process of developing my react app with MUI framework, I encountered various challenges in creating a sticky footer at the bottom of my screen. After exploring different solutions, one approach that I found most satisfactory is as follows: export de ...

"Seeking guidance on getting my carousel functionality up and running in Angular 8 - any

I tried implementing a carousel from the Bootstrap 4 documentation, but it is only displaying one image. How can I modify the carousel to show all images? I am new to using Angular. Below is the code I have: <div class=" bg-success text-white py-5 tex ...

Is there a way to prevent users from downloading PDFs or docs when they are opened in a new tab in a React application?

I recently encountered a situation where I needed PDF files to open in a new tab, but restrict the download for certain users. Despite trying out various third-party packages in React, none of them successfully rendered the PDF files as required. If poss ...

Is there a way to assign the chosen option from a dropdown list to an input field in Vue 3?

I am working with a list that is returned from an API request. I have a text input field where I can search for items in the list, and the results are displayed dynamically as I type. My goal is to be able to select an option from the list and display the ...