0

I have a nested click event listeners and I have a problem, that when I trigger the event listeners multiple times they get looped and send a request x2 times than it should. Where is the mistake in my code? It's probably a beginner's mistake, so I'm very sorry if that's the case. I'm still learning JavaScript and jQuery.

Code:

    $('body').on('click', '.color-pick-btn', function() {
      id = $(this).data("highlight");
      unique = $(this).data("unique");

      $(".color-picker").show();
      //show the menu directly over the placeholder
      $(".color-picker").position({
        my: "right top",
        at: "right bottom",
        of: this,
        collision: "fit"
      });

      $('body').on('click', '.color-picker-item', function() {
        color = $(this).data("color");
        $.ajax({
            type: "POST",
          url: '/echo/json',
          //data: "highlight=" + id + '&color=' + color + "&unique=" + unique,
          data: {
          json:'{"highlight":"id","color":"color","unique": "unique"}'
          },
          dataType: 'json',
        }).done(function(data) {
          console.log('test');
        });
        $(".color-picker").hide();
        return false;
      });

      $(document).click(function(event) {
        if (!(($(event.target).hasClass('color-pick-btn')) || ($(event.target).hasClass('color-picker-item')))) {
          $(".color-picker").hide();
        }
        return false;
      });
      return false;
    });

JSFiddle here: https://jsfiddle.net/o0r4z6g3/

Check out the console output "test".

Cheers!

1 Answer 1

1

You added event listeners every time your event listener was called. I haven't taken a look at the intention of your code, and you haven't explained it in your answer, but this may fix your problem.

$('body').on('click', '.color-pick-btn', function() {
  // .position() uses position relative to the offset parent, 
  // so it supports position: relative parent elements
  pos = $(this).offset();
  id = $(this).data("highlight");
  unique = $(this).data("unique");

  // .outerWidth() takes into account border and padding.
  width = $(this).outerWidth();
  $(".color-picker").show();
  //show the menu directly over the placeholder
  $(".color-picker").position({
    my: "right top",
    at: "right bottom",
    of: this,
    collision: "fit"
  });

  return false;
});

$('body').on('click', '.color-picker-item', function() {
  color = $(this).data("color");
  $.ajax({
    type: "POST",
    url: '/echo/json',
    //data: "highlight=" + id + '&color=' + color + "&unique=" + unique,
    data: {
      json: '{"highlight":"id","color":"color","unique": "unique"}'
    },
    dataType: 'json',
  }).done(function(data) {
    console.log('test');
  });
  $(".color-picker").hide();
  return false;
});

$(document).click(function(event) {
  if (!(($(event.target).hasClass('color-pick-btn')) || ($(event.target).hasClass('color-picker-item')))) {
    $(".color-picker").hide();
  }
  return false;
});
Sign up to request clarification or add additional context in comments.

4 Comments

You're absolutely right. Can I ask how is it possible that the first click event can pass the variable "id" and "unique" to the second event listener when they are two different functions
@popcorn you could use a global variable (not the ideal solution), but without more information about what this code is doing and what unique and highlight represent, it is very difficult to give you a better answer.
Although, you could just do const id = $('.color-pick-btn').data("highlight"); in the second event listener.
That's what I want - to pass the variables from first function to the second, that's why I thought it has to be nested. However, the const id = $('.color-pick-btn').data("highlight"); won't work with multiple buttons. The answer above it enough for me, thanks

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.