0

I'm styling links as buttons, and have noticed that the link action is overridden by the form action. So a cancel button actually updates. Is there a way to remove this behaviour easily and reliably(across all browsers) from the button, while keeping it inside the form element? Or do I need to restyle the buttons?


update:

<form>
 <input type='text'>
 <a href="cancel"><button>cancel</button></a>
 <input type="submit">
</form>

right now the a>button has the same action as input(submit)


ended up restyling and removing the button tags

Daniel
  • 34,125
  • 17
  • 102
  • 150

3 Answers3

1

Sorry if I have the wrong end of the stick here but I am not entirely sure I understand your question as that cancel button shouldnt be submitting your form, you are however using buttons incorrectly which may be the source of your problem.

I think if you do it the way you are doing it they wont work in IE:

<input type="button" value="Cancel" onClick="window.location='http://www.yoursite.com/cancellink'" />

If you do them as above should do whatever you want in all browsers unless js is disabled - hope this helps Instead of linking to a page you could call any js function inther to clear form or whatever you want

SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • While the js being disabled is unlikely, having the button do the exact opposite when it is disabled seems too risky. – Daniel Aug 09 '12 at 17:23
  • I agree if that is the case but it shouldnt submit the form without js - it shouldnt do anything – SwiftD Aug 09 '12 at 17:26
1

You need to set the type of the button, by default button act as a submit button inside a form if type attribute is not set.

<a href="cancel"><button type="button">cancel</button></a>
kiranvj
  • 32,342
  • 7
  • 71
  • 76
0

Because of the way links work, when you click <a href="cancel">, it will try to navigate the browser to that resource. (e.g. http://yoursite.com/cancel).

If you want your button to do some JS action, remove the <a>. It's just redundant in this case. Style the <button> how you want it.

If you want your button to navigate to a page, remove the <button> and style the <a>.

Ansel Santosa
  • 8,224
  • 1
  • 28
  • 39
  • that's what I was afraid of having to do. – Daniel Aug 09 '12 at 17:23
  • Is there a reason you don't want to do this? Nesting a button in a link doesn't make much sense... – Ansel Santosa Aug 09 '12 at 17:34
  • I was using a template for quick prototyping. Now I've gone and styled the links to not use buttons, luckily most of the links are generated using a function(anticipating that I'll be changing the styling) so It's a quick change. I'll have to go through the code and replace the instances where I used the button tags. – Daniel Aug 09 '12 at 18:04