Search code examples
javascriptjquerycssdynamicunderline

Dynamic underlining of active page


Asked a similar question a while ago and tried to build off of my answer but am still having trouble.

I've got a navigation menu that links to different places within the page. I'd like the active pane's link to be underlined. See the jsFiddle for demonstration. The return false is a necessary part of the code. I have a javascript function guiding the page to the location instead of jumping to it instantly.

Thank you!

http://jsfiddle.net/danielredwood/aBuZu/3/

HTML

<div id="nav">
    <a href="#about" id="nav_about">ABOUT</a><br />
    <a href="#pictures" id="nav_pictures">PICTURES</a><br />
    <a href="#contact" id="nav_contact">CONTACT</a>
</div>

CSS

a, a:active, a:visited {
    color:#1d1d1d;
    text-decoration:none;
}
a:hover {
    text-decoration:underline;
}

JavaScript

$('#nav a').click(function(){
    $('#nav a').css('text-decoration', 'none', function(){
        $(this).css('text-decoration', 'underline');
    });
    return false;
});

Solution

  • Try this http://jsfiddle.net/aBuZu/1/

     $('#nav a').click(function(){
        $('#nav a').css("textDecoration", "none"); 
        $(this).css('textDecoration', 'underline');
        return false;
     });