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='">Cancel</button> </form>But it doesn't work. Any ideas?
012 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=""> Cancel </a> … possibly with some CSS to make it look like a button.
1There is no button type="cancel" in html. You can try like this
<a href="">Cancel</a> You can make it look like a button by using CSS style properties.
4There are a few problems here.
First of all, there is no such thing as <button type="cancel">, so it is treated as just a <button>. This means that your form will be submitted, instead of the button taking you elsewhere.
Second, javascript: is only needed in href or action attributes, where a URL is expected, to designate JavaScript code. Inside onclick, where JavaScript is already expected, it merely acts as a label and serves no real purpose.
Finally, it's just generally better design to have a cancel link rather than a cancel button. So you can just do this:
<a href="">Cancel</a> With CSS you can even make it look the same as a button, but with this HTML there is absolutely no confusion as to what it is supposed to do.
3it defaults to submitting a form, easiest way is to add "return false"
<button type="cancel" onclick="window.location=' false;">Cancel</button> 2<input type="button" onclick="window.location.replace('your_url')" value="Cancel" /> 4Just put type="button"
<button type="button"><b>Cancel</b></button> Because your button is inside a form it is taking default value as submit and type="cancel" doesn't exist.
2There is no button type cancel
To achieve cancel functionality I used DOM history
<button type="button" onclick="window.history.back();">Cancel</button>1I know this is old, but I wanted to offer my view on this.
What I tend to do, for better or worse, in trying to prevent use of inline 'onclick' code, is the following:
- Assign type="button" to the cancel button within a form, then
- set a href="/wherever_page_needs_to_go" attribute (alt tags, titles, etc. which are not included in the code below),
- set a specific class to the button, e.g. "btn-cancel", so that it can be selected if a specific need requires it (although in the code below the class is not needed for selection, it may be required for css styling, though),
- link to a JavaScript code in a separate file.
Code for btn-events.js (for me generally located in /public/js folder):
// Select all form buttons with href attribute in DOM const buttons = document.querySelectorAll('form button[href]'); // Add event listeners for selected buttons buttons.forEach(btn => { const href = btn.getAttribute('href'); btn.addEventListener('click', evt => window.open(href, target = '_self')); }); Then in HTML:
<button type="button" href="/wherever">Cancel</button> Also, in HTML just before the ending body tag, link the script:
<script src="js/btn-events.js"></script> This method allows me to control some of the other buttons I occasionally have within the form and which do not submit anything but are purely "navigational" in nature.
<button onclick=\"window.location='{$_SERVER['PHP_SELF']}';return false;\">Reset</button> Not enough rep to Vote Up for Kostyan. Here's my final solution (needed a reset button).
Thanks again to Kostyan for answering the question as asked without suggesting a "workaround" (time-consuming) method to "construct a button" with styles.
This is a Button (which the viewer expects to see) and it works exactly as requested. And it mingles with the other buttons on the page. Without complexity.
I did remove the "type=cancel" which apparently was useless. So even less code. :)
3Here, i am using link in the form of button for CANCEL operation.
<button><a href="main.html">cancel</a></button> 1Thats what i am using try it.
<a href="index.php"><button style ="position:absolute;top:450px;left:1100px;height:30px;width:200px;"> Cancel </button></a> With Jquery:
$(".cancel-button").click(function (e) { e.preventDefault(); });