Click here to Skip to main content
15,884,537 members
Articles / jQuery

How to Avoid Double Clicking With jQuery ?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
19 Aug 2013CPOL1 min read 60.3K   3   7
Here is how to avoid double clicking with jQuery

Introduction

There has been a troubling issue regarding user interaction, when the user double clicks the DOM elements. Such as always buttons, links, etc. Fortunately, jQuery has an awesome solution for this. That is .one() method

What does .one Method Do?

It attaches a handler to an event for the elements and that handler is executed at most once per element.

Theory

.one( events [, selector ] [, data ], handler(eventObject) )

events

Type: String

  • One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin"

selector

Type: String

  • A selector string to filter the descendants of the selected elements that trigger the event
  • If the selector is null or omitted, the event is always triggered when it reaches the selected element

data

Type: Anything

  • Data to be passed to the handler in event.data when an event is triggered

handler(eventObject)

Type: Function()

  • A function to execute when the event is triggered
  • The value false is also allowed as a shorthand for a function that simply does return false

Example

JavaScript
$("#saveBttn").one("click", function () {
    alert("This will be displayed only once.");
});

OR:

JavaScript
$("body").one("click", "#saveBttn", function () {
    alert("This displays if #saveBttn is the first thing clicked in the body.");
});

Key points of the above code are listed below:

  • After the code is executed, a click on the element with Id saveBttn will display the alert
  • Subsequent clicks will do nothing
  • This is equivalent to ==>
JavaScript
$("#saveBttn").on("click", function (event) {
    alert("This will be displayed only once.");
    $(this).off(event);
});

In other words, explicitly calling .off() from within a regularly-bound handler has exactly the same effect.

Do You Need to Know More ?

Conclusion

The above mentioned method works since jQuery Version 1.7. So if your element's click event fires more than once, then this may be the solution. Enjoy this awesome method and let me know if you have any issues.

You Might Also Like

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Sri Lanka Sri Lanka
Sampath Lokuge holds a Bachelor of Science degree in the Mathematics and Computer Science at the University of Colombo, Sri Lanka.

He possesses over 8 years of experience in constructing web applications using Microsoft Technologies including ASP.net MVC, C#, SQL Server, Web API, Entity Framework and also other web technologies such as HTML5, CSS3,jQuery and AngularJS.

Sampath has earned Microsoft certifications such as MCP, MCAD, MCSD and MCTS and very recently he has completed MS (Microsoft Specialist) for MVC 4 and MCSD (Windows Store Apps Using HTML5).

Besides that, he is an active blogger, writing about web and mobile development issues and promoting best practices.He also actively participates in online communities such as Code Project and StackOverflow.He himself is handling three communities, which are ASP.net MVC 5 With C# on Linkedin,Entity Framework 6 on G+ and Hybrid Mobile App with WinJS on Facebook.

Now, I am a 100% Freelancer. Smile | :)

Tech Blogs


Sampath Lokuge Tech Universe

Communities which I'm Handling :


Entity Framework 6

ASP.net MVC 5 With C#

Hybrid Mobile App with WinJS

Comments and Discussions

 
QuestionAnother ques Pin
Ý Nhi Trần Thị7-May-18 21:22
Ý Nhi Trần Thị7-May-18 21:22 
QuestionThanks! Pin
Member 1139809523-Jan-15 9:13
Member 1139809523-Jan-15 9:13 
AnswerRe: Thanks! Pin
Sampath Lokuge23-Jan-15 18:50
Sampath Lokuge23-Jan-15 18:50 
My Pleasure Smile | :)

My Latest Article : NDepend - Static Analysis Tool for .NET Managed Code

Nothing is Impossible for Willing Heart.

GeneralMy vote of 5 Pin
Rakesh Meel26-Aug-13 2:59
professionalRakesh Meel26-Aug-13 2:59 
GeneralRe: My vote of 5 Pin
Sampath Lokuge26-Aug-13 3:05
Sampath Lokuge26-Aug-13 3:05 
GeneralMessage Closed Pin
20-Aug-13 4:33
user558320820-Aug-13 4:33 
GeneralRe: My vote of 5 Pin
Sampath Lokuge20-Aug-13 4:37
Sampath Lokuge20-Aug-13 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.