Solved

Unexpected Result from jQuery toggleClass

Posted on 2012-04-07
6
406 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…

773 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