[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jQuery Menu

Posted on 2013-01-31
3
Medium Priority
?
315 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 1000 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 45

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

830 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