Unexpected Result from jQuery toggleClass

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
MinnRickAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BuggyCoderCommented:
$(document).ready(function () {
            $('div').click(function () {
                $('div').toggleClass('unselected');
                $(this).toggleClass('selected');
            });
        });
0
MinnRickAuthor Commented:
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
BuggyCoderCommented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MinnRickAuthor Commented:
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
BuggyCoderCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MinnRickAuthor Commented:
Great, that answers my question nicely.  Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.