user3985746
user3985746

Reputation:

How to keep the submenu open in the page load?

I want to keep the submenu open at the page load itself. How can I make it possible?

Here is the jquery:

( function( $ ) {
$( document ).ready(function() {
$('#cssmenu li.has-sub>a').on('click', function(){
        $(this).removeAttr('href');
        var element = $(this).parent('li');
        if (element.hasClass('open')) {
            element.removeClass('open');
            element.find('li').removeClass('open');
            element.find('ul').slideUp();
        }
        else {
            element.addClass('open');
            element.children('ul').slideDown();
            element.siblings('li').children('ul').slideUp();
            element.siblings('li').removeClass('open');
            element.siblings('li').find('li').removeClass('open');
            element.siblings('li').find('ul').slideUp();
        }
    });
});
} )( jQuery );

Any help will be greatly appreciated!!

Upvotes: 2

Views: 920

Answers (1)

Ram
Ram

Reputation: 144689

If clicking on the first a element "opens" the "submenu" you can trigger the click event on DOM ready event:

$('#cssmenu li.has-sub>a').on('click', function() {
    // ...
}).first().click();

If you want to select a different a element you can use the .eq() method:

$('#cssmenu li.has-sub>a').on('click', function() {
    // ...
}).eq(2).click(); // trigger the click event on the third `a` element in the collection

Without using .first() or .eq() method the event will be triggered for all the elements in the set. You could also use the .trigger('click') or .triggerHandler('click') method.

Upvotes: 1

Related Questions