2

I made button for selecting timeslot. And I would like to make when second-click, have a time slot on first click and second click and transfer this data. for example ) first click on time slot "12:00 -13:00" and second click on time slot "13:00-14:00" then firsttimeslot = "12:00 -13:00", secondtimeslot="13:00-14:00" I try to use count on button, but it doesnot work!

<button class ="btn btn-success book" data-timeslot="12:00 - 13:00 " count ="1"> 12:00 -13:00</button>

    $(".book").click(function(){
    var count = $(this).attr('count')
    if(count =="1")
        {
    var firsttimeslot = $(this).attr('data-timeslot');
    $(".book").attr('count') = "2";
        }
        else{

      var secondtimeslot=$(this).attr('data-timeslot');

      $("#timeslot").val(firsttimeslot.trim());
      $("#secondtimeslot").val(secondtimeslot.trim());

        }
      }
    )

  </script>

1 Answer 1

1

Try using your counter variable outside of the function so it has a global scope. That way you can use it to track the state of which count you are on. See below:

var count = 0;

$(".book").click(function(){
    count = count++;

    if(count == 1)
    {

        var firsttimeslot = $(this).attr('data-timeslot');

        $(".book").attr('count') = "2";
    }
    else if( count > 2) {
        count = 0; // reset count after second click
    }
    else {

        var secondtimeslot=$(this).attr('data-timeslot');

        $("#timeslot").val(firsttimeslot.trim());
        $("#secondtimeslot").val(secondtimeslot.trim());
        }
      }
    )

You increment count whenever you run the function, and once you pass the second click you reset the counter to start tracking again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.