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

0

I'm trying to create a calendar in JS.

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

Here's an example of how I'm doing to recognize the clicks:

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

Are you able to control the number of clicked times to recognize only two clicks?

    
asked by anonymous 27.07.2018 / 00:01

1 answer

1

Solution

Here is 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 var global within your click function. It will serve to count the number of times it was clicked, so just check the value of the variable with the desired amount, and if the check is true, you execute the commands you want in click . >     

27.07.2018 / 00:12