0

Clicking on one of the navitem classes should hide the div with ID "hammenu"; however it doesn't hide. The CSS does have the display set to "flex" and I tested that condition. Still does not want to hide. Any suggestions would greatly be appreciated.

$(document).ready(function() {
  $(".hammenucntr").click(function(e) {
    var pos = $(this).position();
    $("#hammenu").css({
      "left": (pos.left + 20) + "px"
    }).show();
  })
  $(".navitem").click(function(e) {
    if ($("#hammenu")){ 
        $("#hammenu").hide();
      }
    })
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="page-wrapper">
  <div id="navm">
    <div class="hammenucntr">
      <i class="fas fa-bars"></i>
      <div id="hammenu" class="navmenucntr">
        <div class="navitem">Home</div>
        <div class="navitem">Test 1</div>
        <div class="navitem">Test 2</div>
        <div class="navitem">Test 3</div>
        <div class="navitem">Test 4</div>
        <div class="navitem">Test 5</div>
        <div class="navitem">Test 6</div>
      </div>
    </div>
  </div>
UPDATE: Ok this question started out as a complete train wreck. I created a fiddle that demonstrates the issue. I apologize for trying to hurry through asking a question.

JS Fiddle

7
  • 1
    if ($("#hammenu){ - missing closing " there. Commented Feb 26, 2018 at 15:01
  • 2
    You have some pretty obvious syntax issues here. I'd suggest always checking the console when some JS code doesn't work as expected. I've edited your question to include an executable snippet which should make the issues easier to see Commented Feb 26, 2018 at 15:01
  • I corrected the conditional issue I believe you are referring to. Console shows nothing with clicking on a navitem. Commented Feb 26, 2018 at 15:02
  • @CBroe - Itchy trigger finger on first post. Edit has been made to correct. Issue still lingering. Commented Feb 26, 2018 at 15:03
  • 1
    Then please edit question, to reflect current/actual code. Plus, make the whole thing into a minimal reproducible example, please, so that we can see what is actually going on. Commented Feb 26, 2018 at 15:05

1 Answer 1

3

Your first click handler $(".hammenucntr").click catch it before $(".navitem").click handler, cause .navitem is child of .hammenucntr. You should use e.stopPropagation() in second handler to stop bubbling event to its parent.

more about event phases: https://javascript.info/bubbling-and-capturing

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.