1

I'm simply creating a nav menu and have a basic unordered list with a "sub-menu" within. Here's the Jquery. The reason for the "active" class and if statement is that I want the slider to remain open if choosing another item on the list, so that it doesn't close and open again each iteration.

The problem part here for me in the code is here.

$('.slider').html( $(this).find('ul') );

The main issue is that on('click',), is only updating the first time it's clicked. On a second click on another item in the menu, it doesn't update itself with the new selection and therefore doesn't show the new sub-menu list? Console log shows that it is undefined. But it should be like the first time?

I don't understand why this is happening or the reasons behind it! Logic tells me the same thing that happens the first time, should keep happening, and therefore work?! Help appreciated before I make a hole in the wall with me forehead!

$(document).ready(function() {

    $('ul > li > ul').hide();
    $('.slider').hide();

    // Menu
    $('.menuNav > ul > li').on('click', function() {

        // if active
        if ( $(this).hasClass('active') ) {
            $(this).parents().children().removeClass('active');
            $('.slider').hide('slide', 200);
        }

        // if unactive
        else {
            $(this).addClass('active');
            $('.slider').show('slide', 200);
            $(this).siblings().removeClass('active');
            // Show related list
            $('.slider').html( $(this).find('ul') );

        }

    });
});

HTML

<div class="menuNav">
  <ul>

    <li><span>item_1</span>
      <div class="sub_position">
          <ul>
            <li>sub-item_1-1</li>
            <li>sub-item_1-2</li>
            <li>sub-item_1-3</li>
            <li>sub-item_1-4</li>
          </ul>
      </div>
    </li>

    <li><span>item_2</span>
        <div class="sub_position">

      <ul>
        <li>sub-item_2-1</li>
        <li>sub-item_2-2</li>
        <li>sub-item_2-3</li>
        <li>sub-item_2-4</li>
      </ul>
      </div>
    </li>

.... etc

  </ul>
</div>
Jquestions
  • 1,650
  • 1
  • 23
  • 30
  • Can you show us how your HTML is structured? You are getting undefined likely because the other `
  • ` you have clicked does not have a nested `
      ` in it, therefore returning undefined.
  • – Terry Aug 06 '17 at 22:47
  • See https://stackoverflow.com/help/mcve – guest271314 Aug 06 '17 at 22:49
  • Sorry, added @Terry for reference. – Jquestions Aug 06 '17 at 22:53
  • actually I see it is working fine here in this jsfiddle: https://jsfiddle.net/amrelgarhy/fw1caq12/ – Amr Elgarhy Aug 06 '17 at 23:11