How can you figure out the highest z-index in your document?
You could call findHighestZIndex
for a particular element type such as <div>
like this:
findHighestZIndex('div');
assuming a findHighestZindex
function that is defined like this:
function findHighestZIndex(elem)
{
var elems = document.getElementsByTagName(elem);
var highest = Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
for (var i = 0; i < elems.length; i++)
{
var zindex = Number.parseInt(
document.defaultView.getComputedStyle(elems[i], null).getPropertyValue("z-index"),
10
);
if (zindex > highest)
{
highest = zindex;
}
}
return highest;
}
How to find the highest z-index within a document no matter what tags they are?
This isn't the most efficient solution, but it should work. jsFiddle.
Please note you have to have a position specified in order for the z-index to return a value.
var highest = -999;
$("*").each(function() {
var current = parseInt($(this).css("z-index"), 10);
if(current && highest < current) highest = current;
});
alert(highest);
How to find element with biggest z-Index?
Don't try to find it - you would need to iterate all your DOM. As you are creating them dynamically, just save a reference to it and update it each time. Or just store the current highest z-index
value in a max
variable.
How to get element attribute based on highest z-index
You could modify the function to return the element with the highest z-index, and then just use .id
to get its id
.
Another issue: You're comparing zindex
and highest
as strings instead of numbers. I've prefixed zindex
with the unary +
operator before comparing, as in your example, if you were to compare a z-index of 9
and a z-index of 1000
, it would believe 9
is greater.
Example:
function findHighestZIndex(elem) { var highest = 0; var highestElement = null; var elems = document.getElementsByClassName(elem);
for (var i = 0; i < elems.length; i++) { var style = document.defaultView.getComputedStyle(elems[i], null); var zindex = style.getPropertyValue("z-index"); var ElementDisplay = style.getPropertyValue("display");
if ((zindex != 'auto') && (+zindex > highest) && (ElementDisplay == 'block')) { highest = zindex; highestElement = elems[i]; } }
return highestElement;}
var elem = findHighestZIndex("zindex");console.log(elem.id + " has the highest z-index.");
div.zindex { z-index: 500; position: absolute; display: block;}
<div id="first-element" class="zindex" style="z-index: 1;"></div><div id="second-element" class="zindex" style="z-index: 3;"></div><div id="third-element" class="zindex" style="z-index: 5;"></div><div id="fourth-element" class="zindex" style="z-index: 100;"></div><div id="fifth-element" class="zindex" style="z-index: 99;"></div>
How to find the highest z-index using jQuery
Note that z-index only affects positioned elements. Therefore, any element with position: static
will not have a z-index, even if you assign it a value. This is especially true in browsers like Google Chrome.
var index_highest = 0;
// more effective to have a class for the div you want to search and
// pass that to your selector
$("#layer-1,#layer-2,#layer-3,#layer-4").each(function() {
// always use a radix when using parseInt
var index_current = parseInt($(this).css("zIndex"), 10);
if(index_current > index_highest) {
index_highest = index_current;
}
});
JSFiddle demo
A general jQuery selector like that when used with an option that returns one value will merely return the first So your result is simply the z-index of the first div that jQuery grabs. To grab only the divs you want, use a class on them. If you want all divs, stick with div
.
Related Topics
How to Check If a JavaScript Object Is a Dom Object
React Useeffect in Depth/Use of Useeffect
How to Listen to the Form Submit Event in JavaScript
Get Hours Difference Between Two Dates in Moment Js
JavaScript Get Xpath of a Node
Getting a Better Understanding of Callback Functions in JavaScript
When Is the Comma Operator Useful
Why Is Value Undefined at .Then() Chained to Promise
Get Unique Selector of Element in Jquery
How to Access the Contents of an Svg File in an <Img> Element
How to Format/Tidy/Beautify in JavaScript
Load HTML File Contents to Div [Without the Use of Iframes]
Javascript: Extract Video Frames Reliably
Populate One Dropdown List Based on the Selection of Other Dropdown List
How to Use JSON File in HTML Code
Html5 Audio Tag on Safari Has a Delay
Run Change Event for Select Even When Same Option Is Reselected