Make a form run a javascript when ENTER is pressed or its button is pressed, without submitting

Duck

I have a form with just one field:

  <form id="formCode" onsubmit="return saveCode()" />
    <label for="fcode">Type Code:</label><br>
    <input type="text" id="fcode" name="fcode" autofocus /><br>
    <input type="submit" value="Submit – Enviar" onclick="return saveCode()" />
  </form>      

This is the Javascript I have on the page:

 document.addEventListener("keydown", function(inEvent){
    if (inEvent.keyCode == 13) { // enter
      if (document.getElementById("fcode").value === "") { return; }  // do nothing
      saveCodeCode();
      return false;
    }
  });

  function saveCode() {
    var codeField = document.getElementById("fcode");
    var value = codeField.value;
    if (value === "") { return false; }
    saveValue(value);
    codeField.value = "Submitted";
    return false;
  }

What happens right now is this: the form is always submitted.

I want this behavior:

  1. the user is inside the textfield and types the code
  2. at this point, I want the user to be able to press ENTER and run saveCode() or
  3. press the submit button and also run saveCode.
  4. I don't want this form to be submitted, just run the javascript method.
  5. if the field is empty I want nothing to be done.
SMAKSS

As @Bergi said earlier the best way to do this is to use event.preventDefault(). So in order to achieve this you need some modification, first of all, it is better to set your event listener to the specific input, then your remaining code for listening to the event should work fine. Then you need to use event.preventDefault() inside your saveCode() (And also after catching enter event) to prevent submitting form, but to make event work as expected you need to avoid using return in your onclick (return saveCode()) method to allow event passing to the function automatically.

UPDATE

Since there were some bugs in the previous method, I just add some modifications to it. Due to the saveCode() reference error I moved above of all of its usage to prevent such a thing, then I removed onclick events (They are not recommended at all it JS anymore) and then add an event listener for submitting event instead.

So your final code should be something like this (If saveValue() is existed and work as well):

var codeField = document.getElementById("fcode");

function saveCode() {
  console.log("saveCode is running!");
  saveValue(value);
  codeField.value = "Submitted";
  return false;
}

document
  .getElementById("fcode")
  .addEventListener("keydown", function(inEvent) {
    if (inEvent.keyCode == 13) {
      // enter
      inEvent.preventDefault();
      if (this.value === "") {
        return false;
      } // do nothing
      saveCode();
      return false;
    }
  });

document
  .getElementById("formCode")
  .addEventListener("submit", function(inEvent) {
    event.preventDefault();
    var value = codeField.value;
    if (value === "") {
      return false;
    }
    saveCode();
  });
<form id="formCode">
  <label for="fcode">Type Code:</label><br>
  <input type="text" id="fcode" name="fcode" autofocus /><br>
  <input type="submit" value="Submit – Enviar" />
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Form not submitting when enter key is pressed

Run Javascript when enter pressed

Run method when enter is pressed

CSS Button doesn't execute form when 'enter' pressed on keyboard

Run javascript function when enter is pressed - JavaScript only no Jquery

How to set focus on next input when the button "Enter" is pressed? (javascript)

Trigger a button click with JavaScript when Enter key is pressed

Detect selected form when Enter key is pressed via jQuery or JavaScript

2 Form Submit buttons - Can I have a Javascript run when only one submit button is pressed

Reading a character without requiring the Enter button pressed

Changing the form action, when a submit button is pressed using JavaScript

add characters when enter button pressed

Submit a certain button when ENTER is pressed

When Enter is pressed Ok Button should be clicked

Submit a form only when 'enter' key is pressed

Submit a form when enter is pressed in a textarea in React?

Writing to a div from a form when enter is pressed

Javascript: How to make fade in and out transition when button is pressed?

hide the form after button form pressed javascript

If a Button is pressed or Enter button pressed Jquery

How to make the process run while the button is pressed?

Javascript to trigger submit when enter is pressed

Call a javascript function when enter key is pressed

Force form to send certain submit button through (when there are multiple) when ENTER key pressed

html & javascript enter button redirects to error page - otherwise duplicates text to chat (only when enter is pressed)

Which method is run when Home button pressed?

Kotlin -Run print statement when button is pressed

Run continuous action when button is pressed

Code won't run when button is pressed