Here's how to do it:
var elements = document.getElementsByTagName("*"); var highest_index = 0; for (var i = 0; i < elements.length - 1; i++) { if (parseInt(elements[i].style.zIndex) > highest_index) { highest_index = parseInt(elements[i].style.zIndex; } }
high_index now contains the highest z-index on the page ... just add 1 to this value and apply it wherever you want. You can apply it like this:
your_element.style.zIndex = highest_index + 1;
Here is another way to achieve the same using jQuery:
var highest_index = 0; $("[z-index]").each(function() { if ($(this).attr("z-index") > highest_index) { highest_index = $(this).attr("z-index"); } });
Again, the same as applying a new index to an element:
$("your_element").attr("z-index", highest_index + 1);
Sheavi
source share