Solved

jQuery Menu

Posted on 2013-01-31
3
305 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 43

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Make checkboxes execute queries on click 8 34
Add somewhat of a wildcard to your javascript 12 30
Validating number not work with decimal 4 24
boostsrap 6 12
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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)

749 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