user3191903
user3191903

Reputation: 245

Run button click event only once further clicks needs to be forwarded to other function

I have a button as follows

<input type="button" id="btn" value="Click Me" />

and I have 2 functions

function event1(){
  alert("1st Time Clicked");
}

function event2(){
  alert("Further Clicks");
}

I want to run event1 function for 1st time when the user clicks on that button and for subsequent requests I need to run event2 function.

I tried the following way

$(document).ready(function(){
    $("#btn").one("click",function(){
        event1();
    });
});

But I can't figure it out how to run event2 function for further clicks.

How Can I do that in Jquery ?

I created Jsfiddle = http://jsfiddle.net/rajeevgurram/d9Z3c/

Upvotes: 0

Views: 346

Answers (2)

Rey Libutan
Rey Libutan

Reputation: 5314

I like booleans so I use

$(document).ready(function(){
    var clicked = false;

    $("#btn").on("click",function(){
        if(!clicked) {
            event1();
            clicked = true;
        } else {
            event2();   
        }
    });
});

P.S. I just wanted to be different from the first answer. XD

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

In the first click handler(using .one()), register a normal click handler so that further clicks will trigger that handler

$(document).ready(function () {
    $("#btn").one("click", function () {
        event1();
        $(this).click(event2)
    });
});

Demo: Fiddle

Upvotes: 3

Related Questions