Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a <button> not submit a form?

I've got a form, with 2 buttons

<a href="index.html"><button>Cancel changes</button></a>  <button type="submit">Submit</button> 

I use jQuery UI's button on them too, simply like this

$('button').button(); 

However, the first button also submits the form. I would have thought that if it didn't have the type="submit", it wouldn't.

Obviously I could do this

$('button[type!=submit]').click(function(event) { event.stopPropagation(); }); 

But is there a way I can stop that back button from submitting the form without JavaScript intervention?

To be honest, I used a button only so I could style it with jQuery UI. I tried calling button() on the link and it didn't work as expected (looked quite ugly!).

like image 393
alex Avatar asked Jul 23 '10 02:07

alex


People also ask

How a button does not submit a form?

The default value for the type attribute of button elements is "submit". Set it to type="button" to produce a button that doesn't submit the form. In the words of the HTML Standard: "Does nothing."

How do you prevent form from submitting when pressing?

To prevent form submission when the Enter key is pressed in React, use the preventDefault() method on the event object, e.g. event. preventDefault() . The preventDefault method prevents the browser from refreshing the page when the form is submitted.

Can a button submit without a form?

So the <form> element can be anywhere else in the hypertext document, be it before or even after the button. The button must not be a descendant of the form.


2 Answers

The default value for the type attribute of button elements is "submit". Set it to type="button" to produce a button that doesn't submit the form.

<button type="button">Submit</button> 

In the words of the HTML Standard: "Does nothing."

like image 63
Josh Lee Avatar answered Sep 24 '22 01:09

Josh Lee


The button element has a default type of submit.

You can make it do nothing by setting a type of button:

<button type="button">Cancel changes</button> 
like image 36
s4y Avatar answered Sep 22 '22 01:09

s4y