1

can anyone tell me how can i find zindex of a div in Google chrome?

document.getElementById(id).style.zIndex; //does not work
Emil
  • 7,220
  • 17
  • 76
  • 135

3 Answers3

0

Since z index is mentioned in the CSS part you won't be able to get it directly through the code that you have mentioned. You can use the following example.

function getStyle(el,styleProp)
{
    var x = document.getElementById(el);

    if (window.getComputedStyle)
    {
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp); 
    }  
    else if (x.currentStyle)
    {
        var y = x.currentStyle[styleProp];
    }                     

    return y;
}

pass your element id and style attribute to get to the function.

Eg:

var zInd = getStyle ( "normaldiv1" , "zIndex" );
alert ( zInd );

For firefox you have to pass z-index instead of zIndex

var zInd = getStyle ( "normaldiv1" , "z-index" );
 alert ( zInd );
harikrishnan.n0077
  • 1,927
  • 4
  • 21
  • 27
0

Use jQuery.

$('#id').css( 'zIndex' )
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
0

That is a standard cross-browser notation that works in my version of Chrome. I would check to make sure each part of your JavaScript returns what you expect before continuing:

alert(document.getElementById(id)); // should return an [object HTMLElement]
alert(document.getElementById(id).style); // should return an [object CSSStyleDeclaration]
alert(document.getElementById(id).style.zIndex); // should return blank (default) or a number

also, how do you know it's set? It could be inherit or auto... (which means it's not explicitly declared for that element).

Also, make sure you run it after the element is available in the DOM.

David
  • 345
  • 1
  • 4