0

I have a link that needs to not take action when I hit cancel. I've tried returning false from the click event, and using e.preventDefault, but I'm not sure if am doing it correctly.

I need the link to not take any action when I click cancel.

<a id="selectInterviewTimeLnk" asp-controller="Admin" asp-action="SelectInterviewTime" asp-route-userName="@Model.UserName">Select Interview Time</a>
$("#selectInterviewTimeLnk").click(function(e) {
  var confirm = confirmInterviewTime();
  if (confirm === false) {
    return false;
  }
})
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Juan
  • 75
  • 9
  • Your link has no action to cancel. If click the link does something the it's likely that another `click` event handler has been added to it in JS, and as such you need to place your code in there. – Rory McCrossan Aug 21 '19 at 14:46
  • @RoryMcCrossan Can you show me what mean in code?? – Juan Aug 21 '19 at 15:00
  • Try taking a look here: [difference between event.stopPropagation and event.preventDefault](https://stackoverflow.com/questions/5963669/whats-the-difference-between-event-stoppropagation-and-event-preventdefault) – James B Aug 21 '19 at 15:04
  • Should I be using `dialog` instead? – Juan Aug 21 '19 at 15:05
  • @Juan not really, as somewhere in your code you will have another `click` event handler, like the one you're trying to add now. You need to put your logic inside that instead of creating this new one. – Rory McCrossan Aug 21 '19 at 15:05
  • @Juan no. The issue isn't the `confirm` or `dialog` you use, but the way the events are attached to the element – Rory McCrossan Aug 21 '19 at 15:06
  • @RoryMcCrossan That's interesting, because I really just need it for the link I described in the page. So you think I have duplicate click event? Because the main problem it seems it that it still navigates to the page even when I hit cancel. – Juan Aug 21 '19 at 15:20

1 Answers1

0

I was able to solve with this:

 $("#selectInterviewTimeLnk").click(function (e) {
                    return confirm("Are sure you would to select this interview time?")
                })
Juan
  • 75
  • 9