Solved

jQuery Menu

Posted on 2013-01-31
3
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

687 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