jQuery Code Snippet: Automatically Set First List Item as Active in a Navigation Menu

using jQuery to select each <li> element within a <ul> and add the ‘active’ class to the first one. This code snippet is using the each function to iterate over each <li> element and applying the ‘active’ class to the one with index 0.
using jQuery to select each <li> element within a <ul> and add the ‘active’ class to the first one. This code snippet is using the each function to iterate over each <li> element and applying the ‘active’ class to the one with index 0.
$(‘ul li’).each(function(i) {
if ( i === 0 ) {
$(this).addClass(‘active’);
}
});