0

I'm trying to build a site with a navigation menu which toggles class upon hovering over the navigation buttons which are not currently "selected" or "active". Here is a simple version of the code:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<style type="text/css">
ul li a.selected {font-weight:bold;}
ul li a.hover {color:yellow;}
div.page {position:absolute;}
</style>

<script type="text/javascript">
$(function(){
    $("div.page:not(:first)").hide();   // hide all pages except the home page

    var menu=$("div.navMenu ul li a");
    menu.click(function(){
        var previous=window.location.hash;
        var selected=$(this).attr("href");
        if (previous != selected) {
            $("div.page"+previous).fadeOut();
            $("div.page"+selected).fadeIn();
            }
        $(this).addClass("selected");
        menu.not(this).removeClass("selected");
    });

    menu.hover(function(){
        $(this:not(.selected)).toggleClass("hover");
    });

});
</script>
</head>
<body>
<div class="navMenu">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
<div class="page" id="home">This is my home page.</div>
<div class="page" id="portfolio">This is my portfolio page.</div>
<div class="page" id="contact">This is my contact page.</div>
</body>
</html>

Dreamweaver indicates that there is a syntax error in the line with

$(this:not(.selected)).toggleClass("hover");

However, I don't see what the syntax error is. (The idea is to toggle the "hover" class only for menu buttons which do not have the "selected" class). Any help would be greatly appreciated.

3 Answers 3

2

I believe it should be:

$(this).not(".selected").toggleClass("hover");
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, the "if.. hasClass" thing doesn't seem to be necessary; as far as I can tell "removeClass" doesn't do anything anyways if the element does not have that class.
Glad you got it working. Don't forget to accept the answer if you're satisfied.
1

It's probably supposed to be $(this).not(".selected").toggleClass("hover"); or so

Comments

0

$(this:not(.selected)).toggleClass("hover"); expression is not valid.

Use something like:

menu.hover(function(){
        if ($(this).hasClass('selected'))
            $(this).addClass("hover");
    }, function() {
        if ($(this).hasClass('selected'))
            $(this).removeClass("hover");
    });

Comments

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.