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 5979121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:36:52+00:00 2026-05-22T21:36:52+00:00

I need it so when I click on a div of class ‘mydiv’, all

  • 0

I need it so when I click on a div of class ‘mydiv’, all divs of that class have a z-index of 1, except for the div I clicked on which as a z-index of 2.

Clicking on the div again needs to change its z-index back to 1.

So far ive come up with the following:

    $('.mydiv').toggle(function() {
        $('.mydiv').css('z-index','1');
        $(this).css('z-index','2');
    }, function() {
        $(this).css('z-index','1');
    });

If you click on a div and then click on it again (returning the z-index to 1) before clicking on another one it works fine.

However if you click on a div, and then click another one without clicking the first one (to toggle it back to z-index 1), sometimes it works and sometimes it doesn’t do anything. Im assuming the problem is in the first part of my code because this:

$('.mydiv').css('z-index','1');

Is not always run before this:

$(this).css('z-index','2');

Is that the problem and if so how can I fix this?
Thanks

UPDATE – Sorry for not thinking of this initially, but ive simplified my question here, the actual page will need to have css positioning animations. So when you click on a div it moves with a smooth animation. I think that means I cant just change the css by toggling a class.
Thanks

–

UPDATE 2 – I thought I had this working, and then I tested in IE8 and 7. IE9 is ok (along with every other browser I tested with) but IE7 and IE8 make all of the images jump around when you click on any of them. Here is the demo (all css and jQuery are within the page):

http://smartpeopletalkfast.co.uk/jquery/basicDemo12-bugfix-3.htm

And here is the jQuery:

    $(".image-div").click(function () {


        var divRefTwo = $(".image-div").not(this);
        $(".image-div").not(this).animate({
                width: '250px',
                left: '0px',
                marginRight: '0px',
                backgroundPosition: '-125px'
            }, 400, function() {
                $(divRefTwo).css('z-index','1');
            });

        if ($(this).css('z-index') == 1) {
            $(this).css('z-index','2');
            $(this).animate({
                width: '500px',
                left: '-125px',
                marginRight: '-250px',
                backgroundPosition: '0px'
            }, 500, function() {
                //
            });
        }
        else {
            var divRef = this;
            $(this).animate({
                width: '250px',
                left: '0px',
                marginRight: '0px',
                backgroundPosition: '-125px'
            }, 500, function() {
                $(divRef).css('z-index','1');
            });
        }

    });

I think whats happening is this: The background image position for div.image-div starts at -125px. When you click a div.image-div, jQuery animates the background position for all the other divs of the the same class to -125px. Only divs that are expanded should change, as the other divs already have background position of -125px.

For some reason IE resets the background position to 0, and then animates to -125px. So the animation ends up in the correct place, but animates to get their when it shouldn’t.

Any ideas why this is happening? Is this a jQuery IE bug or a CSS hierarchy of selectors thing?
Thanks

  • 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-22T21:36:53+00:00Added an answer on May 22, 2026 at 9:36 pm

    So now we changed everything again. Based on the OP edit, now the code would be:

    $(".mydiv").click(function () {
        var $t = $(this);
        $t.siblings().css("z-index", 1).animate({
                "margin-top": 0
            }, "fast");
        if ($t.css("z-index") == 1)
            $t.css("z-index", 2).animate({
                "margin-top": -10
            });
        else
            $t.css("z-index", 1).animate({
                "margin-top": 0
            }, "fast");
    });
    

    Here is the again updated working sample.

    Now let me explain the logic of the code.

    // Since we'll use $(this) (the clicked div) in many places
    // of the code, I started caching it in a var named $t.
    var $t = $(this);
    
    // Then I get all the siblings (The other divs that are beside this,
    // or are children of the same parent). The I change back all this divs to
    // z-index = 1 and animate their top down to 0.
    $t.siblings().css("z-index", 1).animate({ "margin-top": 0 }, "fast");
    
    // Next we have the if statement to check if the clicked div is with
    // z-index = 1 (unclicked) or z-index = 2 (already clicked).
    if ($t.css("z-index") == 1)
    
    // Then we change the current div to z-index = 2 and animate it 10px upper.
    $t.css("z-index", 2).animate({ "margin-top": -10 });
    
    // Or else we change back the current div to z-index = 1 and animate it down.
    $t.css("z-index", 1).animate({ "margin-top": 0 });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a div layer so that when you click on it
I need a div to be disabled (so that it no longer receives click)
I have a hidden button on a form that I need to click in
I have the following set up: <div class=click red></div> <div class=click green></div> <div class=click
I have the 2 divs on a page which I need to animate up
I have several divs with one class on a page. <div class=main/> <div class=main/>
I need to repeat a div on click under the initial div that is
I would really need to programmatically click on all nodes in the collection, but
I need a function that a user can just click a flash app button
I need to make a whole div(with class container) editable when the user clicks

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.