I am making a navigation bar for my client panel. It has many links on it. When a link is clicked, it will change what is shown in the main section of the screen. I need to be able to change the classes on each li entry in the ul (navigation bar).
This is my code, and it works:
$(document).ready(function() {
function changeActive() {
$('#elem1, #elem2').removeClass("active");
}
$('#main-section').load('src-pages/elem1.html');
$('#elem1').addClass("active");
$('li#elem1').click(function() {
//load file now
$('#main-section').load('src-pages/elem1.html');
changeActive();
$(this).addClass("active");
});
$('li#elem2').click(function() {
//load file now
$('#main-section').load('src-pages/elem2.html');
changeActive();
$(this).addClass("active");
});
});
I am trying to make the file size as small as possible and would like to add the $(this).addClass("active"); line, into to the changeActive function. When I do add that line into the function, it doesn't work properly and only applies the :hover and :focus styles to the li or a element(s).
Could someone explain:
- Why that line (
$(this).addClass("active");) doesn't work in the function - How I could fix that line in order to put it inside the function
Thank you in advance.