I have a static menu in a common header file. Whenever I visit any of the pages by clicking on the links in the menu, I need an image over that clicked link as an indicator of the activated link.
below is the HTML of menu I am using
<ul id="in-menu">
<li><a href="unleashing-your-heart" >home</a></li>
<li><a href="fromdaniella" >from Daniella</a></li>
<li><a href="material-list" >material list</a></li>
<li><a href="program" >program</a></li>
<li><a href="testimonials-2" >testimonials</a></li>
<li><a href="#">login</a></li>
</ul>
How to use jQuery or javascript to make the desired functionality work?
You don’t need javascript/jQuery for this
What you can do is to give each of your pages a unique ID or Class on the
<body>element or main containerdivsomewhere near the top of the the HTML structure anyway.. then give each of the links in your menu a unique ID or class too (if using classes they can be the same as the first one)e.g.
So on your Home page the
bodyclass might benm-unland your login page would havebodyclassnm-logetc.. the menu itself never changes so it can still be in a common fileThen in the CSS each link can be specifically targeted.. so say your plain link does not have an image, but the
:hoverand your “current” pages doThen you would group the new/specific selectors into the hover rule selectors, those rules are then more specific than the ordinary
#in-menu a {}rule, and also they will only ever apply to your “current page link” i.e. when the two classes are the same on a pageYou still need the
#in-menuID in the selector as well as the two classes because otherwise the selector will not have enough weight to override the default ruleA jQuery solution would work in a similar way, the logic would be the same. You would still need a unique page indicator i.e. a body class or ID then you would check the body ID/Class and add a current class to the relevant, matching link.