How to use jQuery to disable a hyperlink image on click in an ASP.NET MVC 2 View

In one of my recent projects, my view displayed a table of hardware alerts for set of monitored devices. On any of the alerts, the user had three potential actions: close, initiate a chat session, or email tech support.

The latter two actions are pretty simple, as they are just hyperlinks that open in a new window. Close Alert, on the other hand, is an MVC ActionLink that goes back to Alert/Close/{Id}.

The problem with this function is that it's not re-entrant. User testing indicated that some people are so accustomed to double-clicking that they would inadvertently try to close the same alert twice. QA asked me to come up with a solution.

Yes, I know I should also guard against this in the controller. However, trying to close an alert doesn't cause any data corruption (it just doesn't work and the user gets an ugly error message). The issue is entirely one of user-experience. Because of that, and the fact that I needed something quick, I turned to jQuery. Our site makes heavy use of jQuery, so JavaScript is already a requirement.

Here is the final code I added to the page.
$().ready(function () {
    $('.closeAlertButton').click(function () {
        $(this).fadeTo(0, .5);
        $(this).unbind("click");
        $(this).click(function () {
            return false;
        });
        return true;
    });
});
Looking at the code line by line:
  1. The first thing you'll notice is that we run this code after the page has finished loading.
  2. The next line sets up a click event handler for all elements with the "closeAlertButton" class, which happens to be all of my "Close Alert" hyperlink images.
  3. The click event handler fades the image, making it appear disabled.
  4. It then unbinds the existing click handler so that additional clicks won't call back in this function.
  5. Then it adds a new click handler that only does a return false. This prevents the hyperlink from working.
  6. Finally, the existing click event handler returns true, allowing the hyperlink to do its thing, which is to cause the Alert/Close/{Id} action to load.
The Alert/Close/{Id} action refreshes the page, so I don't have to worry about cluttering up the browser with a bunch of disabled image buttons.

There are other ways I could have done this. One thought I had was simply removing the hyperlink target. I probably should use jQuery to call the Alert/Close/{Id} action asynchronously, without a full page load, but I'll still need to prevent multiple submissions. Perhaps displaying a modal "progress" dialog would be appropriate. The solution presented here worked for our purposes, is relatively clean, and has minimal impact to our existing code base. Furthermore, it is self-contained enough to be a reusable pattern if we need it on other pages. I post it here in the hopes that it might be of some use to others.

Comments

Popular posts from this blog

How to copy your Frozen Free Fall progress to a new phone

Ionic vs. Bootstrap - for a Web App

How I Finally Got AdMob and Ionic Framework to Play Nice Together