3

How can I place a button inside a form which when clicked will not submit the form?

Demo HTML:

<form> <button>TEST</button> </form> <div></div> 

Demo JavaScript (jQuery):

$(document).ready(function() { $('form').submit(function() { $('div').append('SUBMITTED<br>'); return false; }); }); 

JSFiddle

When the button is clicked the form gets submitted. Is there a way to disable this behavior (rendering this demo form unsubmittable)?

My actual use case for this is creating a bootstrap styled button to activate a file input.

0

2 Answers 2

6

By default <button> type is submit, so just make it button:

<form> <button type="button">TEST</button> </form> 

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. Funny that you have to specify button twice rather than submit once.
1

There is the tag <button> in HTML. You could use something like this:

<button type="button">This is the button label</button> 

You can check the definition and usage here: http://www.w3schools.com/tags/tag_button.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.