Cameron Bass Cameron Bass - 3 months ago 11
jQuery Question

JQuery page load: Active class

I have some JQuery that will set a class to active onclick. but when I load the page nothing is "Active" not until I click one of the list items. Is there a way that one can be select upon the page load?

Current JQuery:

$(".setup-nav").on( "click", "li", function() {
$(".setup-nav li.active").removeClass("active");
$(this).addClass("active");
});

Answer

Just add active to your the HTML item.

<ul class="setup-nav">
    <li class="active">This is your fist active item</li>
    <li>Second Item</li>
</ul>

And, to prevent keeping the last change by soft reloading the page which could be happened by some browsers, just use this:

$(".setup-nav li.active").trigger('click');
Comments