How to create a . click() limiter in JS?

Asked

Viewed 80 times

0

I’m trying to create a JS calendar.

First I made a <table> that will fill the <td> according to the days, leaving all available to click and select a particular day. The user selects a round day and a round day (changing the style of the date selected by clicking) and the dates are redirected to a particular date <input>. And there’s the problem, when you click on several <td>, they keep changing the style without a click control.

This is an example of how I’m doing to recognize clicks:

function daysD(){
    $("td").click(function (){
        this.style.color = "#FFF";
        this.style.backgroundColor = "#65BC2B";
    });
}

Has the ability to control the number of times clicked, to recognize only two clicks?

1 answer

1


Solution

See an example of how it could be done:

  $().ready(function() {
    var limit = 0

    $("td").click(function () {
        if (limit++ < 2) {
          this.style.color = "#FFF";
          this.style.backgroundColor = "#65BC2B";
        }
    });
  })

Explanation

You can use a var global within its function of click. It will serve to count the number of times it was clicked, with this, just make a check of the value of the variable with the desired amount, and if the check is true, you execute the commands you want in click.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.