How to create a HTML Cancel button that redirects to a URL

Red Cricket picture Red Cricket · Aug 23, 2013 · Viewed 202.2k times · Source

I am playing with the Buttons in the w3schools Tryit editor, and I am trying to figure out how to make my browser redirect to an URL when I click on the "Cancel" button.

Here's what I have tried:

But it doesn't work. Any ideas?

Answer

Quentin picture Quentin · Aug 23, 2013

cancel is not a valid value for a type attribute, so the button is probably defaulting to submit and continuing to submit the form. You probably mean type="button".

(The javascript: should be removed though, while it doesn't do any harm, it is an entirely useless label)

You don't have any button-like functionality though, so would be better off with:

<a href="http://stackoverflow.com"> Cancel </a>

… possibly with some CSS to make it look like a button.