# Cancel button option on form?

**URL:** https://forums.knack.com/t/cancel-button-option-on-form/12333
**Category:** Get Answers
**Created:** [April 4, 2022, 3:42pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333 "2022-04-04T15:42:54Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![WilliamPorter](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/williamporter/32/1354_2.png) [@WilliamPorter](https://forums.knack.com/u/WilliamPorter)
#### Post date: [April 4, 2022, 3:42pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/1 "2022-04-04T15:42:54Z")

</div>

I want to add a Cancel button to my forms (so users can change their minds and exit the form without saving anything they’ve changed). I searched for this topic and found a feature request from six years ago but as far as I can tell, it has not ever been implemented. This seems pretty basic so I think I must be missing something. Please tell me I’m missing something.

---

<div class="post-metadata">

### Author: ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)
#### Post date: [April 4, 2022, 5:37pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/2 "2022-04-04T17:37:33Z")

</div>

Hi @WilliamPorter,

I normally have my add or edit forms set as modal popups so the user can click X at the top to close. If you haven’t set the modal to not automatically close when clicked outside (requires JavaScript), the user can simply do that and it will close without saving.

I may be missing or misunderstanding something though 🤔

---

<div class="post-metadata">

### Author: ![WilliamPorter](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/williamporter/32/1354_2.png) [@WilliamPorter](https://forums.knack.com/u/WilliamPorter)
#### Post date: [April 4, 2022, 7:03pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/3 "2022-04-04T19:03:54Z")

</div>

> [@CarlHolmes](#):
>
> I normally have my add or edit forms set as modal popups so the user can click X at the top to close. If you haven’t set the modal to not automatically close when clicked outside (requires JavaScript), the user can simply do that and it will close without saving.
> 
> I may be missing or misunderstanding something though

Carl,

Thanks for the reply. No, I don’t think you’re missing anything.

But I have two responses.

First, I’ve had some little problems with modal dialogs in Knack as form pages: It _seems_ to me that the original table page sometimes does not get updated immediately after the form is submitted and that modal window closed. I need to test this more carefully, though. Not sure what I’m seeing.

Second, yes, the close box will work, but not all users understand that. And more important, it’s valuable to have an explicit cancel option. An awful lot of stuff on the internet and in desktop apps too now _saves records automatically_. I start a message in my email app or Google documents, then simply close my browser — and the document I was working on is saved. So an explicit “Cancel” button (meaning “exit and save nothing”) is a clarifying option to have. I would note that Knack provides Cancel buttons itself in the builder. 🙂

So I guess I should submit this as a feature request.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/8/85206a93aa4ae00f87d5d4658fc8fb0c14ab0b3e.png)

---

<div class="post-metadata">

### Author: ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)
#### Post date: [April 4, 2022, 7:30pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/4 "2022-04-04T19:30:15Z")

</div>

Re your first point, if you set the “Form Rules” to “Redirect to the parent page” instead of “Show a confirmation message”. This will close the form and then refresh the underlying table. If you just close a form that adds a record you will need to refresh the page to show the new entry.

I almost always change the rule from showing “Form successfully submitted” to redirect to parent.  
You can see this on the below video which starts at 4min 40secs

[![](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/5/5c15d92f7c0ffc0e3669989dd11beb9674f9ec10.jpeg "Modal Popup - Knack Database") ](https://www.youtube.com/watch?v=Gkq_ERDgHUQ)

---

<div class="post-metadata">

### Author: ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)
#### Post date: [April 4, 2022, 7:40pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/5 "2022-04-04T19:40:54Z")

</div>

Re your second point, I’m sure there is a coder that can provide you with a snippet of JavaScript so you can create a cancel button. I’m not a coder 😊

---

<div class="post-metadata">

### Author: ![WilliamPorter](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/williamporter/32/1354_2.png) [@WilliamPorter](https://forums.knack.com/u/WilliamPorter)
#### Post date: [April 4, 2022, 7:56pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/6 "2022-04-04T19:56:15Z")

</div>

> I’m not a coder.

Heh, me, neither, not any more. 🙂

Thanks for the excellent tips.

---

<div class="post-metadata">

### Author: ![KnackPros](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/knackpros/32/807_2.png) [@KnackPros](https://forums.knack.com/u/KnackPros)
#### Post date: [April 5, 2022, 7:20am UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/7 "2022-04-05T07:20:52Z")

</div>

> I’m sure there is a coder that can provide you with a snippet of JavaScript so you can create a cancel button.

Here it is. You can customize on which forms the button appears by tweaking the first few lines.

```auto
// Add this to your Custom JavaScript

// Cancel Button
$(document).on('knack-view-render.form', function(event, view, data) {
  // Show the button on the following form types:
  const actions = ['create', 'update', 'insert']

  // Optional: Exclude specific forms
  const exclude = ['view_1', 'view_2']

  if (!actions.includes(view.action) || exclude.includes(view.key)) {
    return
  }

  const $submitBtn = $('#' + view.key + ' button[type="submit"]')
  const cancelBtn = '<button class="kn-button my-cancel">Cancel</button>'
  const added = $('#' + view.key + ' button.my-cancel').length !== 0

  if (!added) {
    // Avoid adding the button to the page twice
    $submitBtn.after(cancelBtn).css('margin-right', '0.75em')
  }

  const $cancelBtn = $('#' + view.key + ' button.my-cancel')

  $cancelBtn.on('click', function(e) {
    $cancelBtn.addClass('is-disabled is-loading')
    e.preventDefault()
    Knack.Navigation.redirectToParentPage()
  })
})

```

---

<div class="post-metadata">

### Author: ![WilliamPorter](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/williamporter/32/1354_2.png) [@WilliamPorter](https://forums.knack.com/u/WilliamPorter)
#### Post date: [April 5, 2022, 3:06pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/8 "2022-04-05T15:06:30Z")

</div>

THANK YOU, KnackPros!

I tried to send you a private message but apparently you’ve got that turned off. I wanted to know if there was any chance I could hire you for, oh, 20-30 minutes.

William

---

<div class="post-metadata">

### Author: ![KnackPros](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/knackpros/32/807_2.png) [@KnackPros](https://forums.knack.com/u/KnackPros)
#### Post date: [April 5, 2022, 5:24pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/9 "2022-04-05T17:24:53Z")

</div>

No worries 👍

Sure. Feel free to schedule a call:

> **[Knack Pros](https://calendly.com/knackpros)**
>
> Welcome to my scheduling page. Please follow the instructions to add an event to my calendar.

That’s strange about the Private Messages, hm.

---

<div class="post-metadata">

### Author: ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)
#### Post date: [April 6, 2022, 5:32pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/10 "2022-04-06T17:32:32Z")

</div>

Nice one Ian (@KnackPros) - I was hoping you’d know the answer 😊

---

<div class="post-metadata">

### Author: ![WilliamPorter](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/williamporter/32/1354_2.png) [@WilliamPorter](https://forums.knack.com/u/WilliamPorter)
#### Post date: [April 6, 2022, 5:51pm UTC](https://forums.knack.com/t/cancel-button-option-on-form/12333/11 "2022-04-06T17:51:59Z")

</div>

It wasn’t the answer I wanted (I wish this were easier) but it’s AN answer and looks to be best available now, so I’ve marked this as the solution. Thanks again to Ian at KnackPros!
