2

I am trying to use jquery to build a top navigation. When a link button/menu is selected the button should be higlighted and display the selected item and when its goes to that page selected.

Is there anyway to do this. I have this script it kinda works but not completely.

    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){ 
        $('#hdcnav ul li a').click(function() {
                $('#hdcnav ul').find('img').each(function(){
                var imgsrc = $(this).attr('src');
                if(imgsrc.indexOf('selected')>=0){$(this).attr('src',imgsrc.split('-')[0]+".gif");}
                });
                $(this).find('img').attr('src',$(this).find('img').attr('src').split('.')[0]+"-selected.gif");
            });
    }); 
    </script>
</head>
<body>
    <div id="nav">
        <ul>
            <li><a href="page1.html"><img src="images2.gif" alt="home" /></a></li>
            <li><a href="page2.html"><img src="images3.gif" alt="" /></a></li>
            <li><a href="page3.html"><img src="images4.gif" alt="" /></a></li>
            <li><a href="#"><img src="images4.gif" alt="" /></a></li>
            <li><a href="#"><img src="images5.gif" alt="" /></a></li>
            <li><a href="#"><img src="images6.gif" alt="" /></a></li>
        </ul>
    </div>

1 Answer 1

1

One thing that I've done that works is to add and remove the classes based on the selection (a click in this case) which is ver easy and straight-forward to do. I then use CSS to use a different image on a selected class so that it appears different than the other menu items around it.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, CSS sprites are the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.