Solved

Unexpected Result from jQuery toggleClass

Posted on 2012-04-07
6
398 Views
Last Modified: 2012-08-14
Hello Experts -
Please consider the following ASP.NET test page..
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="toggle_class.aspx.vb" Inherits="toggle_class" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body { font-family:arial; font-size:12px; }
        .selected { color:red; text-decoration:underline; font-weight:bold; margin-bottom:5px; }
        .unselected { color:#5a5a5a; text-decoration:none; font-weight:normal; margin-bottom:5px; }
        .unselected:hover { cursor:pointer; text-decoration:underline; margin-bottom:5px; }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.unselected').click(function () {
                $('.selected').toggleClass('selected unselected');
                $(this).toggleClass('selected unselected');
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="selected">Block #1</div>
    <div class="unselected">Block #2</div>
    <div class="unselected">Block #3</div>
    <div class="unselected">Block #4</div>
    </form>
</body>
</html>

Open in new window

My problem is with the behavior of the first element after one of the other elements has been selected.  Even though the first element's class (properly) toggles to 'unselected' the click event won't subsequently fire for it.  The event fires fine for the other elements, it's only the first element that has this issue.  Can someone please tell me why this is occurring?  Thanks.

-- Rick
0
Comment
Question by:MinnRick
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37819850
$(document).ready(function () {
            $('div').click(function () {
                $('div').toggleClass('unselected');
                $(this).toggleClass('selected');
            });
        });
0
 

Author Comment

by:MinnRick
ID: 37819953
Yep, that works, as does adding a second class to each element and triggering the click event by that second class.  I'm still puzzled though why my initial solution doesn't work.  Can you help me understand what prevents that first element from being able to trigger the click event in my initial question?  Thanks.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37820003
the first element has selected class applied to it, and in your code you are attaching the click event to the elements with unselected class....
reason why the click event is not attached to your first element....

hope its clear now...
0
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!

 

Author Comment

by:MinnRick
ID: 37820118
Nope, not clear.  After one of the other elements is clicked the class for the first element becomes 'unselected' (so far so good).  Then, with the 'unselected' class applied to the first element, that first element should trigger the 'unselected' click event when it is clicked, right?  It doesn't.
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37820122
no it will not because the event has already been attached with the div that has unselected class.
bear in mind that its the div to which the click event will attach and not class. class name is just a selector....

if you see i have modified the code to add click event to all the divs rather than only div with certain class.
Just to ensure that all the divs have their click event binded with a function, where in we can call toggleClass to toggle the behaviour on selection....
0
 

Author Closing Comment

by:MinnRick
ID: 37820128
Great, that answers my question nicely.  Thank you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

706 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

22 Experts available now in Live!

Get 1:1 Help Now