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

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

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.

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!
Vu 32'' TV Vu 24'' TV
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!