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
Solved

jQuery Menu

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to combine and minify CSS and JS files 5 17
Add shadow behind div 5 25
multiple selects 23 48
Html Newline 7 20
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

856 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