skip to Main Content

The callback function code refused to render on my console as expected…what could be the issue?


FormEl = document.getElementById ("form"); 
FormEl.addEventListener( "Submit",() =>{
Const userInput = inputNum.value
Console.log (userInput)
})

I was expecting to get back the value typed into the input field element after clicking the submit button on the html.

2

Answers


  1. // Assume you have an input element with id="inputNum"
    const formEl = document.getElementById("form");
    const inputNum = document.getElementById("inputNum");
    
    formEl.addEventListener("submit", (event) => {
      // Prevent the default form submission behavior
      event.preventDefault();
      
      // Get the value from the input field
      const userInput = inputNum.value;
      
      // Log the input value to the console
      console.log(userInput);
    });
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Form Example</title>
    </head>
    <body>
        <form id="form">
            <input type="text" id="inputNum" placeholder="Type something">
            <button type="submit">Submit</button>
        </form>
        
        <script src="your-script.js"></script>
    </body>
    </html>

    Replace "your-script.js" with the path to your JavaScript file. With these corrections, clicking the submit button should now log the input value to the console without reloading the page.
    You can use it .

    Login or Signup to reply.
  2. event.preventDefault();
    please add this and try.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search