views:

175

answers:

7

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 of 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!).

+3  A: 

Just use good old HTML:

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

Wrap it as the subject of a link, if you so desire:

<a href="http://somewhere.com"&gt;&lt;input type="button" value="Submit" /></a>

Or if you decide you want javascript to provide some other functionality:

<input type="button" value="Cancel" onclick="javascript: someFunctionThatCouldIncludeRedirect();"/>
JGB146
Using a standard button control with proper type attribute is "good old html," and creates much simpler markup.
R0MANARMY
It's clearly not simpler if in some browsers it has a default type of submit and in others it has a default type of button. Heck, even just having the default type of submit complicates things more than is necessary IMO. There should be markup that easily provides a button that does nothing. And there is: `<input type="button" />`
JGB146
+1 I've used this exact technique lots of times, and it has always worked well for me.One variant is if you need to cancel the postback event for a server-side button based upon some client-side calculation, you can include window.event.returnValue = false; in your code that executes in the client-side onclick event for your button... that is, if using a custom validator control doesn't cut the mustard for you :)
Adam McKee
@JGB146: Just because not all browsers default to the same value doesn't mean they won't respect the correct type if it's set manually (as is suggested by jleedev. Not to mention the questions specifically asks for a way to do it **without JavaScript** while your answer doesn't take that into account.
R0MANARMY
@R0MANARMY: I gave multiple options in my answer. The first one involves no JavaScript. Is there really a significant difference between `<input type="button" />` and `<button type="button" />` in your eyes? To me, the only difference is that `<input />` **provides more functionality** should you wish to employ it.
JGB146
And to clarify, by "more functionality" I mean by supporting things like specifying size, alt text, disabling, etc. I honestly don't know if these things are in `<button />` because I do not use it. <a href='http://www.w3schools.com/tags/tag_button.asp'>W3Schools</a> does not list them for button, but obviously they do <a href='http://www.w3schools.com/tags/tag_input.asp'>for input</a>
JGB146
`button` does have more support for styling. It's also easier to select in IE6, i.e. `button { }` against `input[type=submit] { }` for single submit buttons. I guess that is IE's fault though.
alex
@JGB146: `Button` is a container in HTML. That allows you to place things like images or tables (not sure why you'd do this, but you could) etc while `input` doesn't support that. There is a difference between the two, and each one has their appropriate use case.
R0MANARMY
@R0MANARMY: Thanks for noting this. As I said, I haven't used `button` so I didn't realize this difference. I would be interested in hearing your take on when only one of them is appropriate though. In fact, I'll start a question about that. After all, there is `<input type="image" />` too, for the situations where you want an image as your button ;)
JGB146
The question already exists: http://stackoverflow.com/questions/1398955/input-typebutton-and-button-whats-the-difference
JGB146
@JGB146: Unfortunately I don't really have a take on it, I knew there's a difference in that buttons are containers, and in what value they submit is not standard across all browsers.
R0MANARMY
A: 
<input type="button" value="Submit" />
<input type="image" value="Submit" src="..." />
Sadat
+6  A: 

The default value for the type attribute of button elements is "submit".

<button type=button>Submit</button>
jleedev
According to w3schools, that's [not true for IE](http://www.w3schools.com/tags/att_button_type.asp).
R0MANARMY
@R0MANARMY: Yes, on IE<8, `button` is the default `type`...
CMS
+2  A: 

Avoid using <button> tag since it has different behaviours on different browsers. Use <input> instead. There are three different buttons in HTML.

Firstly, normal buttons

<input type="button" value="Click here to do something" />

Secondly, reset buttons which reset the form

<input type="reset" value="Click here to reset" />

And lastly submit buttons which submit the form

<input type="submit" value="Click here to submit" />

You can also make customized functions for each type, using javascript and input element events.

Hamid Nazari
I use `button` as a submit widespread and it works over all browsers. I just guess it doesn't work too good at *not* submitting forms.
alex
+2  A: 

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>
Sidnicious
A: 

Honestly, I like the answers above. Easy and no need to get into JS. But I noticed that you were asking about jQuery. So for the sake of completeness, in jQuery if you return false with the .click() handler, it will negate the default action of the widget.

See here for an example (and more goodies, too). Here's the documentation, too.

in a nutshell, with your sample code, do this:

<script type="text/javascript">
    $('button[type!=submit]').click(function(){
        // code to cancel changes
        return false;
    });
</script>

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

As an added benefit, with this, you can get rid of the anchor tag and just use the button.

Tim
A: 
<input type="submit" disabled="disabled" />
<input type="button" disabled="disabled" />
iroel
That won't work, http://jsbin.com/exano3/edit (at least in Firefox)
alex
I've tested in firefox 3.5.7 and it worked. What firefox version did you use?
iroel
@iroel I tested it in 3.66. BTW, I didn't downvote you.
alex