Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

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!");
    $(this).off(e);
});

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.


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.


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.


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.


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!
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!

x