Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unexpected Result from jQuery toggleClass

Posted on 2012-04-07
6
Medium Priority
?
414 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

596 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