user1876234
user1876234

Reputation: 857

Launch event on first click jQuery

Is there a possibility in jQuery to add event e.g alert('hi'); for first click only ?

Upvotes: 2

Views: 869

Answers (3)

Meta_data
Meta_data

Reputation: 608

The Jquery one binding solution will be good for you as long the dom is not realoded. i.e the user is on the same page without refreshing.

if you need to use this including page reloading there are 3 ways:

1.using a DB 2.using a Session 3.using a Cookie

those are all server side solutions, allowing you to control the user click events. if this is what you are looking for i will elabrate this and will write code examples. if not the one binding suggestion is good. hope this help.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382150

Yes, you may use the one binding function :

myelement.one('click', function() { 
    alert('hi');
});

Upvotes: 9

Kolby
Kolby

Reputation: 2865

You can make a boolean to check if it's the first click like this:

var clicked = false;

$(document).click(function() {
    if(!clicked){
          alert("hi");
          clicked = true;
    }
});

Upvotes: 1

Related Questions