Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:49:30+00:00 2026-05-29T11:49:30+00:00

I have about 30 clickable divs on my page so I figured I would

  • 0

I have about 30 clickable divs on my page so I figured I would use their class name to trigger click events.

$('div.select_statement_option_on').live('click', function() {
    $(this).attr('class', 'select_statement_option_off');
});
$('div.select_statement_option_off').live('click', function() {
    $(this).attr('class', 'select_statement_option_on');        
});

Now I have come up against a barrier where in one of the 35 divs is clicked, some extra work is done so I added this

$('div#sel_total_cost').click(function() {
    if ($(this).attr('class', 'select_statement_option_off')) {
        $('div#sel_reg_type').attr('class', 'select_statement_option_on');
        $('div#sel_days_reg').attr('class', 'select_statement_option_on');
        $('div#sel_add_tickets').attr('class', 'select_statement_option_on');
        $('div#sel_reg_date').attr('class', 'select_statement_option_on');
        $('div#sel_pcode_disc').attr('class', 'select_statement_option_on');
    }
});

So in the #sel_total_cost div, if I click it from it’s initial state (select_statement_option_off), the ID click function is fired, followed by the class click function. The trouble comes when I try to click it to turn it off. The ID function is fired, and then for some reason the class of the div is changed to be ‘off’ (without the ‘on’ class function firing) and then the ‘off’ class function is fired and the div goes back on. That’s what I see from stepping through, but to watch it, it basically means you can’t turn it off once it’s on. How can I sort out this order of events?

Answer from lonesomeday

Turns out that the if statment was assigning the class rather than checking to see if my div had that class. Updated if statment below

$('div#sel_total_cost').click(function() {
    if ($(this).hasClass('select_statement_option_off')) {
        $('div#sel_reg_type').attr('class', 'select_statement_option_on');
        $('div#sel_days_reg').attr('class', 'select_statement_option_on');
        $('div#sel_add_tickets').attr('class', 'select_statement_option_on');
        $('div#sel_reg_date').attr('class', 'select_statement_option_on');
        $('div#sel_pcode_disc').attr('class', 'select_statement_option_on');
    }
});

Also noted is to not use ‘live’ to bind elements anymore. It is deprecated and performs poorly by comparison to the newer ‘.on()’ function in jQuery 1.7 so I have changed my handlers to look like this

$('div#select_statement_box').on('click', 
    'div.select_statement_option_off', function() {
    $(this).attr('class', 'select_statement_option_on');
});

Where select statement box is the container with my divs in it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:49:31+00:00Added an answer on May 29, 2026 at 11:49 am
    if ($(this).attr('class', 'select_statement_option_off')) {
    

    That doesn’t do what you think. It sets the class to select_statement_option_off, then returns the jQuery selection. (See attr.) This will always be truthy, so the conditional will always pass. The class will also always be select_statement_option_off, hence the other behaviour.

    You need to do a comparison. You could do this with ===:

    if ($(this).attr('class') === 'select_statement_option_off') {
    

    Better would be to let jQuery do the work and use hasClass:

    if ($(this).hasClass('select_statement_option_off')) {
    

    This has an additional advantage: if your HTML changes in future to add other classes (it’s entirely possible!) then this last option will not require changing the Javascript. jQuery abstracts this away for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 50 p tags and next to these are again 50 divs.
I'm about to have to write a web page/app that will serve the agenda
I have a question about creating and managing events inside an ascx custom web
I have an xml file I use to show the About AlertDialog in my
I have a PHP-generated page which is displaying some data about a set of
I have this code <div id=c01 class=hider> < a href=flash.swf class=bump>flash</a> </div> and it
i have a question about a function. function &formatName($id, $name, $clanId, $clanSym, $clanCol) {
I have a DIV with many elements that are all clickable. Each one with
Given I have: $(a.clickable).livequery('click', (function (e) { var values = $(this).attr('id').split('_'); $('#' + values[3]).load($(this).attr('href'));
I have a page with multiple divs that hold different sections of content. Initially,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.