Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sanitize Runaway Z Indexes!

DZone's Guide to

Sanitize Runaway Z Indexes!

·
Free Resource
This is a function I wrote to sanitize zIndexes of a list of elements that you may continually are bringing to the top of a page.  This function goes through HTML Elements and makes all the numbers as low as can be without affecting ordering. 

Feel free to make suggestions or to use this code how you'd like. 

One condition, please just tell me if you're using it, and if you have ideas for improvement, let me know
kenny[@t  ]]]standsolid.com

function sanitizeZ(elements){

	function sortByZIndex(a, b){
		return a[1] - b[1];
	}

	var tempZArray = new Array();
	for(i in elements)
		tempZArray.push([i, elements[i].style.zIndex]);

	tempZArray.sort(sortByZIndex);

	for(i in tempZArray)
		elements[ tempZArray[i][0] ].style ['zIndex'] = i;

}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}