0


I have a issue where the .click from the div where my cancel button is nested in triggers when I click the button. This causes both the slideUp and slideDown to trigger at the same time, which results in the div staying visible. I've tried adding a state to prevent the div from sliding down again, but this does not have the desired effect.

$(add).click(function () {
 var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
 if(state == 0){
   $(inputDiv).slideDown(300);
  }
 state = 1;
});
 $(cancel).click(function () {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).slideUp(300);
  state = 0;
});

https://jsfiddle.net/kkdoneaj/2/

Does anyone know how to work around this issue?

2

3 Answers 3

1

Use Event.stopPropagation() to stop the click from bubbling from the #cancel button to the parent #add div:

$("#cancel").click(function(e) {
    e.stopPropagation();
    ...
});

https://jsfiddle.net/kkdoneaj/3/

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

Comments

0

you should put stop

Stop the currently-running animation on the matched elements.

$(add).click(function () {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).stop().slideDown(300);
});
$("#cancel").click(function (e) {
  var inputDiv = Polymer.dom(root_root).querySelector("#inputDiv");
  $(inputDiv).stop().slideUp(300);
  e.stopPropagation();
});

5 Comments

Thanks for your answer, sadly doing this locks the div in the down position.
@Michelindoll I do not understand what you mean
When testing with stop included in the code the div will slideDown just fine, but it refuses to move up again when the cancel button is pressed.
@Michelindoll Try again with the update of my answer
@Michelindoll The code in the fiddle (previous to your modifications based on this answer) should be in the question itself.
0

Others have commented with stopPropagation(), but you can also not expand #inputDiv unless it's already visible, like so:

$(function(){
  $("#add").click(function () {
    if($("#inputDiv").css('display') == 'none'){
      $("#inputDiv").slideDown(1000);
    }
  });
  $("#cancel").click(function () {
    $("#inputDiv").slideUp(1000);
  });
});

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.