Tips to successfully utilize addEventListener on a submit action

Having issues getting this to work on submit, it functions properly when using

document.getElementById("gets").addEventListener("click", b_button);

but does not work when I try

document.getElementById("gets").addEventListener("submit", b_button);

Is there a way to make it work on submit instead of using click?

<form method="post">
          
    <button type="submit" id="gets">Submit</button>
    <p id="error"></p>
            
   
</form>
    
   
<script>
function b_button() {
    document.getElementById("gets").disabled = true;
    var time_meter = 10;
    var runTimer = setInterval(function() {
        if (time_meter <= 0) {
            clearInterval(runTimer);
            document.getElementById("error").innerHTML = "";
        } else {
            document.getElementById("error").innerHTML = 'counting ' + time_meter;
        }
        time_meter -= 1;
    }, 1000);
    setTimeout(function() {
        document.getElementById("gets").disabled = false;
    }, 10000);

}

// Change the event listener from click to submit
document.getElementById("gets").addEventListener("submit", b_button);
</script>

Answer №1

Make sure you're listening to form submission, not just button submission

<form id="form" method="post">

document.getElementById("form").addEventListener("submit", function(e){ .... }

Remember to include e.preventDefault(); in your code to prevent unintentional submissions

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

Tips for Placing a Div in a Precise Location

I'm currently working with Bootstrap and I'm attempting to replicate a layout similar to the one used by Twitter, as shown in the screenshot below. The specific part I'm struggling with is where the profile picture is located. I want to add ...

The collapsible tree nodes overlap one another in the D3.js visualization

I'm currently working on integrating a d3 code into Power BI for creating a collapsible tree structure. Each node in the tree is represented by a rectangular box, but I've run into an issue where nodes overlap when their size is large. Below is t ...

Clicking on a button within the parent element will enable you to remove the parent element multiple times with the use of VanillaJS

Within a ul element, each li contains multiple elements in the following structure: <ul> <li> <div> <p>some text </p> <button>delete</button> <div> </li> <li> ...

"Implement a function in Node.js/JavaScript that creates a new JSON object if the ID matches with the ID of another

const data = [ { system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", content: { type: { name: "Author" } }, }, DataDetails: { shortSlugOption: { "en-us": "some value", "za-op": "random value" }, ...

Revamp the style of the date field in a Material UI dialog

I'm currently working on a React project where I am using Material-UI's date picker component to display date items. However, I find that the default style does not meet my requirements. I would like to use a selector for displaying dates in the ...

The jQuery included does not function properly in production mode and results in an error stating that it is not a function

After placing the jquery.placeholder.js file in the assets/javascripts folder, I successfully applied the function in my coffeescript file and it worked perfectly. $(document).ready -> $('input, textarea').placeholder(); However, when swit ...

Creating a dynamic 3D pie chart with Highcharts and JSON data

I am attempting to create a 3D pie chart using HighChart with JSON data. Despite enabling the 3D option, it only displays in 2D. Here is an example of the 3D pie chart I am trying to achieve: Could someone please help me identify what might be missing in ...

Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements f ...

Looking for a way to prevent a div element from scrolling in JQuery Mobile?

Here is my implementation: http://jsfiddle.net/coderslay/Lhb5Y/ I have content structured like this: <div data-role="content"> <div><!--Contains a button --></div> <div><ul>..<!--Contains a list -->...</ ...

Because of the CSS tree structure, it is not possible to add or remove classes. This restriction applies to all actions done in jQuery, including click

I want to create a hover effect where the CSS changes when hovering over an item, then reverts back to normal when no longer hovered. Additionally, I would like the CSS to change when the item is selected, and return to normal when another item in the same ...

Having trouble accessing the menu in the dropdown div when clicking back?

I am working on creating a menu that drops down from the top of the page using JQuery. I have everything set up with the code below: <div id="menu"> <div id="menucontentwrapper"> <div id="menucontent"></div> </di ...

What is the best way to integrate JavaScript libraries into my NPM build process?

My current website is built using HTML and CSS (with SCSS) and I have been using an NPM build script. Now, I am looking to incorporate some JavaScript libraries into my site, such as lozad. I have already downloaded the necessary dependencies for the libr ...

Iterate through each element in the array and manipulate the corresponding data

I am trying to figure out a way to assign a unique id to each item in an array and send it to the "script.js" file for individual data processing. <script href="script.js"></sciprt> script.js: $(function(){ $('#box' ...

Images that have been resized on Android appear blurry when viewed on the second page

Customizing the Toolbar: #main_bar { background: url(../images/logo.jpg) 99% 50% no-repeat; background-size: auto 80%; } The original size of logo.jpg is 220x266px The toolbar has a fixed height of 46px Therefore, the background image appears t ...

How can we convert milliseconds to the corresponding date and time zone in Java?

1)I am trying to determine the user's timezone and current time using the following code snippets: Calendar currentdate1 = Calendar.getInstance(); TimeZone tz = Calendar.getInstance().getTimeZone(); System.out.println("time zone"+tz); System.out.pri ...

Chrome displaying an extJs Button image

Could it be that Chrome is evolving into the new IE in terms of CSS issues? Here is the code I have for creating ExtJS buttons within an accordion: var button = Ext.create('Ext.Button', { text: '<img src="'+resp.sellers.externa ...

My custom class is being ignored by Tailwind CSS within breakpoints

I'm attempting to incorporate some animation on the height property within the md breakpoint, but for some reason Tailwind CSS isn't applying my class. <div className={`h-12 bg-blue flex w-full text-white fixed mt-1 md:bg-white ${scrolling ? ...

The JQuery countdown timer seems to be stuck and isn't decreasing as expected

I've been working on a jQuery countdown timer that should start at 30:00 minutes and count down to 00:00. However, I'm facing an issue where it gets stuck at 29:59. Here's the code snippet I'm using: var now = new Date(); var newDateOb ...

Using jQuery delegate with anchor elements in Internet Explorer has proven to be

My website is functioning flawlessly on Firefox and Chrome, however in IE 8 (I have yet to test on 7) some of the .on delegates are not working. Specifically, the ones used on anchor tags. Here is the HTML: <ui id='daylist'> <li& ...

URLs not being gathered by the web scraping tool

Currently involved in scraping data from this specific website to compile a database. Previously, I had functioning Python code available on GitHub that successfully accomplished this task. However, due to a major overhaul in the HTML structure of the site ...