Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

<form action="demo_form.asp" method="get">    First name: <input type="text" name="fname"><br>    Last name: <input type="text" name="lname"><br>    <button type="submit" value="Submit">Submit</button>    <button type="reset" value="Reset">Reset</button>    <button type="cancel" onclick="javascript:window.location='http://stackoverflow.com';">Cancel</button>  </form>

But it doesn't work. Any ideas?

like image 353
Red Cricket Avatar asked Aug 23 '13 16:08

Red Cricket


People also ask

How do I create a redirect button in HTML?

By using HTML Anchor Tags <a>.. </a>, you can Redirect on a SIngle Button Click [html button click redirect]. To use HTML Anchor tags to redirect your User to Another page, you need to write your HTML Button between these HTML Anchor Tag's starting <a> and Closing </a> Tags.

How do you create a button that opens a link in HTML?

Using button tag inside <a> tag: This method create a button inside anchor tag. The anchor tag redirect the web page into the given location. Adding styles as button to a link: This method create a simple anchor tag link and then apply some CSS property to makes it like a button.

How do you set a cancel button?

On any Windows Form, you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key, regardless of which other control on the form has the focus.


2 Answers

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.

like image 187
Quentin Avatar answered Oct 27 '22 01:10

Quentin


There is no button type="cancel" in html. You can try like this

<a href="http://www.url.com/yourpage.php">Cancel</a> 

You can make it look like a button by using CSS style properties.

like image 42
RJ Anoop Avatar answered Oct 27 '22 02:10

RJ Anoop