1

When I click .dot element it should query database and show the result, and in second click it should hide the result. However it just show nothing in first click.

When I leave second if empty it successfully queries the database and show the result. But when I add this code $(".conte").hide(); to second if it shows nothing in click.

here is the code:

$(document).ready(function() {
  var do1 = 0;
  var do2 = 0;
  var do3 = 0;
  var do4 = 0;

  $('.dot1').click(function() {

    if (do1 == 0) {
      do2 = 0; // for other function
      do3 = 0; // for other function
      do4 = 0; // for other function
      do1 = 1;

      $.ajax({
        type: "POST",
        url: "query1.php",
        success: function(data) {
          $(".conte").html(data);
        }
      });
    }
    if (do1 == 1) {
      $(".conte").hide();
      do1 = 0;
      //hide me
    }
    // $(this).toggleClass('clicked');
  });
});
3
  • Have you looked in the browser console for errors? Commented Jul 5, 2019 at 20:09
  • Found the mistake after 5 hours, it should be else if Commented Jul 5, 2019 at 20:15
  • @Tabriz - If you think you found your answer and it is useful and not just a syntax error, I suggest putting your solution in an answer and marking it as a solution. Otherwise, I would delete the question. Commented Jul 6, 2019 at 1:35

1 Answer 1

1

Before you send the AJAX request in the first if, you do do1 = 1;. So when you then test if (do1 == 1), this is successful, so you hide .conte.

Change the second if to else if, so you don't run both blocks in the same click. Or use else if these are the only two possible values.

If you're using these for binary states, it's better style to use true and false, not 0 and 1. Then you can write:

do1 = true;

if (do1) {
    do1 = false;
    ...
} else {
    do1 = true;
    ...
}
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.