1

I'm absolutely trying to steer clear of plugins for this, I want something lightweight and simple. This is as far as I have gotten on a contextual menu. I can click on the li.dd a and it reveals the ul, and when I click on it again it hides the ul. What it doesn't let me do is click on anchors in the ul. Instead of linking to that anchor it hides the menu. Anything I need to do differently?

html:
  <ul class="tools">
    <li class="dd"><a href="#" class="bnt">Tools</a>
      <ul>
        <li><a href="#">Item 1</a></li>
        <li><a href="#">Item 2</a></li>
        <li><a href="#" class="last">Item 3</a></li>
      </ul>
    </li>
  </ul>

SASS:
  .tools
    font-weight: bold
    margin: 12px 0 -12px !important
    text-align: right
    a.bnt
      border: #404041 1px solid
      color: #fff !important
      padding: 6px 15px
    ul
      background-color: #fff
      margin-top: 20px !important
      margin-left: 106px !important
      width: 210px
      a
        border-bottom: #cfcfcf 1px solid
        color: #666
        display: block
        font-weight: normal
        padding: 6px 20px
        text-align: left
        &:hover
          background-color: #000
          color: #fff
        &.last
          border-bottom: none

jQuery:
$(function() {
  $(".dd").toggle(
    function(){ 
      $("ul", this).fadeIn("fast");
      $(this).addClass('on');
          }, 
    function(){ $("ul", this).fadeOut("fast"); $(this).removeClass('on'); }
  );
});
1
  • I've tried plugins but they add extra markup that I don't want, plus some just don't do it exactly how I need it. Commented Sep 17, 2010 at 22:43

1 Answer 1

1

Since you attached the click event to the list item that contains the anchors, that's to be expected. You can either put a dedicated element to handle the toggle events, move the event to the first anchor outside of the inner list-items, or stop propagation of click events on the anchors -

$('.dd li a').click(function(e) {
   e.stopPropagation();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Cool, worked like a charm, and I also changed the selector to not use Shizzle. 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.