Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 buttons on form only 1 with submit type but both triggers submit event

Not using jQuery, just plain javascript with DOM. There are 2 buttons on the form, one has no type, one has type="submit"

Here is an example of my question:

https://plnkr.co/edit/wAlpawx2wJUZ9FXWXL9h?p=preview

         <div>
            <button class="add">add</button>
        </div>
        <div>
            <button type="submit">submit</button>
        </div>

both buttons trigger the submit event. how do I get only the button with type="submit" to trigger the submit event?

Also, how do I persist the form data so that the result can stay in the "pre" tag when the submit is triggered?

like image 230
Annie C Avatar asked Oct 20 '25 02:10

Annie C


1 Answers

set the type attribute to "button" or manually cancel the event

<button type='button' class='add'>add</button>

-or-

$('button.add').click(function(e){
    e.preventDefault(); // cancel default behavior
    // my add logic
});
like image 144
Steve Avatar answered Oct 22 '25 17:10

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!