Solved

jQuery Menu

Posted on 2013-01-31
3
295 Views
Last Modified: 2013-02-01
Hello Experts -
I have a VERY simple ASP.NET page in which I am attempting to toggle between two menu items using jQuery.  The page code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
    <style type="text/css">
        .sel, .unsel{ margin-left:25px; font-size:10px; color:blue; }
        .sel { text-decoration:none; font-weight:bold; }
        .unsel { text-decoration:underline; font-weight:normal; }
        .unsel:hover { cursor:pointer; }
    </style>
    <script src="js/jquery-1.8.1.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
        <span class="sel">Menu 1</span>
        <span class="unsel">Menu 2</span>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.unsel').click(function () {
                $('.sel, .unsel').toggleClass('sel').toggleClass('unsel');
            });
        });
    </script>
</body>
</html>

Open in new window


All I want the thing to do is toggle the menu items' CSS from bold/underline to non-bold/non-underline with each click of the non-active (underlined item).  The problem is that the click event is only firing when the 'Menu 2' item is clicked, not the 'unsel' item.  The logic is absurdly simple, but I can't seem to get it to work.  What am I doing wrong?

I'm using IE8, BTW..

One other thing, perhaps unrelated but about which I'm also curious..  I've noticed some odd markup in the generated source for the 'Menu 2' item.  It looks something like this:
<SPAN class="unsel" jQuery181020724113781910175="1">Menu 2</SPAN>

Open in new window

What is that long jQuery##.. attribute?

Rick
0
Comment
Question by:MinnRick
3 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 250 total points
ID: 38842406
try this:
$(function () {
            $('.unsel, .sel').on("click", function () {
                if ($(this).hasClass('unsel')) {
                    $('.sel').toggleClass("sel unsel");
                    $(this).toggleClass('unsel sel');
                }
            });
});

Open in new window

Your answer to weird jquery attribute: http://stackoverflow.com/questions/3316991/jquery-is-adding-a-strange-attribute-to-nodes
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 38843852
It's not working because you bind the click event to elements with the 'unsel' class when the page first loads. You then add the 'unsel' class to a different element but that doesn't have the click event bound because it didn't have the 'unsel' class when the page was first loaded! Make sense? That's why you need to use the on() function instead of the click() function.

An alternative approach, instead of turning off one class and turning on another, is to simply add and remove a 'selected' class as needed. Needs a slight change to your styling, but have a look at this:

http://jsfiddle.net/ChrisStanyon/TYtc9/
0
 

Author Closing Comment

by:MinnRick
ID: 38844211
Both replies were most helpful, thank you very much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now