views:

289

answers:

5

I have an unordered list.

<style>
  #button1 { position:relative; z-index: 3; }
  #button2 { position:relative; z-index: 2; }
  #button3 { position:relative; z-index: 1; }
</style>

<ul>
  <li id="button1">Button 1</li>
  <li id="button2">Button 2</li>
  <li id="button3">Button 3</li>
</ul>

I am currently using css to give each id a different z-index so that one button is slightly on top of the next. What I want to do is to use jQuery to add a class that add's a much higher z-index to whatever button was clicked such as '999' to ensure that it will be on top of the other two buttons. I can't figure out how to add an active class to the currently clicked <li> while removing the active class from whatever button currently has it set. I don't know how to make jQuery figure out which of the other two <li>'s has the active class set before the new button is pushed. How would I go about doing that?

Also I am not sure if adding a class will solve the problem since I am already setting the z-index by targeting the <li>'s id. Maybe I would need to affect the inline css for the currently pushed button so it overrides the id's css, and also have it check to see which of the other two has the inline css z-index set to '999' so it can remove their inline css and have a larger z-index than the other two buttons and appear on top?

+1  A: 

if I understand correctly, should be

$('li').click(function(){
    $('.active').removeClass('active');
    $(this).addClass('active')
});

Edit - what you're experiencing is a CSS problem - #id rules take precedence to .class rules.

To answer your question, you can set z-index using:

$(this).css('z-index', 999);
Kobi
Close, but that doesn't work, since I already have id on each of those buttons and the id overrrides the class for some reason, so when the active class sets, it still doesn't change the z-index
zeckdude
Is there any way i can set the inline css on the currently clicked button while removing the inline css from the other two buttons?
zeckdude
Try being more specific in your css. For example, li.active, or ul li.active so that you're more specific than the style for the ids. Also, it's not good practice to just assign z-index: 999. That just invites someone else to assign z-index: 9999; somewhere else. There's no reason you can't look at your code and look at all the z-index assignments and just go one higher, or at lease count by 5's or something if you want to ever 'sneak one in' later on to do somethign else.
George Sisco
+2  A: 

To take Kobi's answer a little bit further. I would give the li(s) some common class if possible or an id on the ul.

var $li = $('li.clickMe'); // or var $li = $('#id li');

$li.click(function(){
    $li.removeClass('active');
    $(this).addClass('active');
});

If you are setting the z-index based on IDs, then the css rule for the ID will likely take precedence over any class based rule. You could as well put the z-index property inline and change it with jQuery.

var $li = $('li.clickMe'); // or var $li = $('#id li');

$li.click(function(){
    $li.css('z-index', 1); // Sets all li(s) to z-index of 1.
    $(this).css('z-index', 999);
});
smack0007
Your solution is even closer, but when it changes one of the li's z-index to 999, it add's the inline css z-index of 1 to the other two, whereas I would just like it to remove the inline css for the other two so it reverts back to their original z-index values.
zeckdude
I think the line: $(this).css('z-index', 999); can be changed to: $(this).css('z-index', ''); and that should remove it. If you really want it to revert back though you should just use classes.
smack0007
+2  A: 

the best practice is to use class, but you can also modify the style property, like

$('li').click(function(){

  var li = $(this);

  // check the highest z-index
  var maxIndex = 4;

  $('li').each(function(){
    var zindex = $(this).css("z-index");
    if ( parseInt(zindex) > maxIndex)
    {
      maxIndex = parseInt(zindex);
    }
  });

  // increment by 1
  maxIndex++;

  li.css('z-index', maxIndex);
});
Anwar Chandra
can't you just save `maxIndex` as a global variable? do you really have to loop every time to get it? Anyway, you get +1, because I've edited my answer before I saw yours.
Kobi
So you're simply making the latest clicked li one z-index higher than the previous clicked li's z-index? That's an interesting solution. Thank you! Does it really need to be so in depth?
zeckdude
@kobi: we can put it that way, but this is the safest way. if we have too many li elements, then we should not loop, make it global var.
Anwar Chandra
A: 

By looking at all you answers and upon finding the siblings selector, I figured out an answer that is simple and works for me. Please tell me if there is anything wrong with it other than I didn't use classes. I think for my situation, it works nicely! Thanks for all your guys' help!

$('li').click(function(){
      $(this).css({'z-index' : '999'})
      .siblings().css({'z-index' : ''});
});

Since smack0007's answer helped me the most to get the answer I am using, I am giving him/her the checkmark.

zeckdude
Him. I think my way would actually be a little bit faster as mine does fever DOM lookups. I have no stats to back that up though and if the speed is fine who cares.
smack0007
A: 

Here is another method... this stores the z-index and replaces it when not the active button.

$(document).ready(function(){
 var indx = $('li[id*="button"]').length;
 $('#button1').addClass('active');
 $('li[id*="button"]').each(function(){
  $(this).css('z-index',indx).data('zindx', indx);
  $(this).click(function(){
   var active = $('.active');
   active.css('z-index', active.data('zindx')).removeClass('active');
   $(this).addClass('active').css('z-index',999);
  })
  indx--;
 })
})
fudgey