Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


How to call click or any event only once in jQuery

Share with WhatsApp

In one my project, there was requirement where I had to perform some activites once user loads the particular div at first time and I had completed that code with some more lines of code as compare to the code which I am sharing now.

You know the on() method of jQuery, if not you can read more about it here, on() method attaches event handlers to the selected element as from jQuery version 1.7 it also provides all functionality required for attaching event handlers including support for old bind(),delegate() and live() method.

Using on() method you can attach event handler to element and using off() method you can remove bound events like below to have functionality of showing or executing an event only once.

$("#btnClick").on( "click", function(e) {
    alert( "This alert will be displayed only once." );
    $(this).val("No use of clicking now!");

You can achieve same functionlity using one() method provided by jQuery and code will get changed like below.

$("#btnClickOne").one( "click", function(e) {
    alert( "This alert will be displayed only once." );
    $(this).val("No use of clicking now!");

Check live fiddle of the above code.

If you check the above code carefully one() method gives same behaviour but there is no need to attach and deattach the event explicitely as it handles it internally.

Hope this simple and short information has added something in your knowledge bank. Thanks.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.

Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.

Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.

How to select all checkboxes as per range of numbers in jQuery MVC

JQuery code to select all checkboxes as per range provided with select all and deselect all functionality which can be embedded in ASP.NET MVC too. With validations such as invalid range.

Speed up coding in Visual Studio with code snippets & samples at your fingertips

Know how you can speed up coding in Visual Studio with Bing Developer Assistant by having millions of code snippets and sample projects at fingertips.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!