views:

28

answers:

1

Assuming an accordion dropdown with the standard form of:

<ul>
  <li>
  <a href="#">Main Element</a>
    <ul>
      <li>
      <a href="#">Dropdown Element</a>
      </li>
    </ul>
  </li>
</ul>

I'm using jQuery to expand when the parent element link is clicked:

var $j = jQuery.noConflict();

function initMenus() {
    $j('ul.menu ul').hide();
    $j.each($j('ul.menu'), function(){
        $j('#' + this.id + '.expandfirst ul:first').show();
    });
    $j('ul.menu li a').click(
        function() {
            var checkElement = $j(this).next();
            var parent = this.parentNode.parentNode.id;

            if($j('#' + parent).hasClass('noaccordion')) {
                $j(this).next().slideToggle('normal');
                return false;
            }
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                if($j('#' + parent).hasClass('collapsible')) {
                    $j('#' + parent + ' ul:visible').slideUp('normal');
                }
                return false;
            }
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
                $j('#' + parent + ' ul:visible').slideUp('normal');
                checkElement.slideDown('normal');
                return false;
            }
        }
    );
}
$j(document).ready(function() {initMenus();});

To add a class to the Main Element when clicked (aka the class is enabled anytime the dropdown is expanded) I'm trying to use .toggleClass(className) without luck, most likely to my positioning. Where can I add this element to get the desired effect?

A: 

Try replacing this line:

var parent = this.parentNode.parentNode.id;

with:

var parent = $j(this).parent().closest('a').attr('id');

And instead of:

$j(this).next().slideToggle('normal');

Try:

$j(this).parent().closest('a').slideToggle('normal');

The closest moves up to find the closest link which in your case is main link.

Sarfraz
This does seem to be a better method but it doesn't seem to address the issue of adding a class to the parent element, which is the current issue I'm struggling with. I'm switching the code over from .parentNode to .parent.
Anders H
You can add the class with `addClass` like this: `$j(this).parent().closest('a').addClass('class_name_here');`
Sarfraz