1

please see the code .

$(".test").on("click", function() {
  var sclass = $(this).attr("class").split(" ")[1];
  $(".color").trigger("click");

  $(".color").on("change", function() {
    var scolor = $(this).val();
    $("." + sclass).css("background-color", scolor);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="color" name="color" class="color"><br><br>
<div class="test test1">hii</div><br><br>
<div class="test test2">hello</div>

here i need to change the background color of the div by selecting color . But when i am selecting color for one div then this color is effected to two ? what is the error in this code ?

3 Answers 3

1

In order to understand this bug, you will need to understand Javascript closures. There's a good link here that explains it, and it reveals this is a pretty easy mistake to make (link to MDN closures).

You're creating closures when you assign these functions to those events , and each closure shares the same environment. Essentially sclass will be equal to test test2 regardless. You're selection in $("." + sclass) will expand to $(.test test2). Also you may need to include quotes in your jQuery selection.

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

2 Comments

While interesting, it's not the issue here. The issue is that using on leaves the event handler behind, so the 2nd click still fires for the first div (ie on 2nd click, there are 2 change handlers rather than just the one for the current div).
For future reference: you've detailed what could be causing the issue, which is great, but you've not provided info on how to fix the issue, ie an answer.
1

You should use off and then on

$(".test").on("click", function() {
  var $el=$(this);
  $(".color").off("change").on("change", function() {
     $el.css("background-color",  $(this).val());
  }).click();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="color" name="color" class="color"><br><br>
<div class="test test1">hii</div><br><br>
<div class="test test2">hello</div>

9 Comments

I'm using one and not on. Do you know the difference?
You're wrong! He can change it when he wants... try my snippet. And please, read more about the meaning of "arrogance".
He needs to change the color only on the div click.
Exactly what he needs. We will see.
So could just use off() with on() :)
|
0

The problem you have is because you add a new change handler to the color input on each click. This means the first handler is attached to the .test1 element when you create a new one on .test2. This is why both elements are then effected by the change.

You need to move the change handler outside of the click handler. You can then just store the reference of the element directly instead of a class. Try this:

$(".test").on("click", function() {
  $(".color").data('el', $(this)).trigger("click");
});

$(".color").on("change", function() {
  var $el = $(this).data('el');
  $el && $el.css("background-color", this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<input type="color" name="color" class="color"><br><br>
<div class="test test1">hii</div><br><br>
<div class="test test2">hello</div>

2 Comments

Doesn't need a global var
could you please check this stackoverflow.com/questions/56404480/…

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.