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

  • SEARCH
  • Home
  • 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 9016741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:04:45+00:00 2026-06-16T04:04:45+00:00

The idea is simple; first add a class to certain divs, then with a

  • 0

The idea is simple; first add a class to certain divs, then with a click function remove 1 of the elements class and recount the total classes.
But while removing the old class and adding the new class i want to use an animation.

HTML:

   <div>1</div>
   <div>2</div>​

CSS:

.divclass{
    width:200px;
    height: 200px;
    border: 1px solid #000000;
    background: #ffffff;
}

.divclass2{
    width: 250px;
    height: 250px;
    background: #000000;
}​

Jquery:

var div = $('div').eq(0);
var div2 = $('div').eq(1);

div.addClass('divclass');
div2.addClass('divclass2');

var totaldivclass = $('.divclass').size();
var totaldivclass2 = $('.divclass2').size();

alert(totaldivclass);
alert(totaldivclass2);    

$('.divclass2').click(function(){
$('.divclass2').animate({
    'width':'200px',
    'height':'200px',
    'background':'#ffffff'
}, function(){$('.divclass2').removeClass('divclass2').addClass('divclass')});

//recount total classes
totaldivclass = $('.divclass').size();

alert(totaldivclass);
});​

This works, tough not the way i want it:
at the first click it does the animation, then afterwards it should add and remove the classes.
then after that it should recount the classes and alert the total.

it does all this, but at the first click it counts all the current classes as 1.
the second click however shows 2..

my question: how do i get it to work so it counts 2 elements (so the correct total) at the first click?

Fiddle:
http://jsfiddle.net/a6mx2/

  • 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-06-16T04:04:47+00:00Added an answer on June 16, 2026 at 4:04 am

    See my fiddle

    The DOM updating isn’t done by the time the code drops out to the recalculation part.

    Moving the recount inside the animate function then shows the correct total.

     function(){        
            $(this).removeClass('divclass2').addClass('divclass')
            totaldivclass = $('.divclass').size();  
            alert(totaldivclass);  });    
    

    So you have a DOM update “threading” issue.

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

Sidebar

Related Questions

I have a simple set of jQuery statements for a particular class: $(.printersToggle).mouseover(function() {
First of all, excuse my poor topic title. I simply have no idea how
The idea is simple. Make a struct for Departments of a store, give it
OK, the idea is simple. Every wordpress site is uses categories and tags taxonomies
So, my idea is quite simple: print all of the possible combinations given X
I'm developing a control which inherits from a System.Windows.Form.Panel. The idea is very simple:
The idea is to write a simple bookmarklet. If the question is a possible
I have no idea how to do this simple task. LightSwitch's Solution Explorer is
a possibly simple problem, but weird why I have no idea how to do
I have a simple task to do using PHP dealing with pagination. The idea

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.