AdamJones
AdamJones

Reputation: 601

jquery to remove all z-index elements of a certain value

I'm trying to use jquery to remove all z-index values within a given selector that match a specific z-index. Does anyone have an idea how to do this?

Thanks for any help!

Upvotes: 0

Views: 539

Answers (2)

theftprevention
theftprevention

Reputation: 5213

Give this a try:

var search = "2"; // Could also be "inherit", etc.
$('.selector').css('z-index', function(i, val) {
    return (val.toString() == search) ? '' : val;
});

See this demo.

This way, if the z-index of the element isn't 2, then it's left alone. If it is 2, then it will be removed; in jQuery, setting a CSS value to a blank string removes the value and defers it to the stylesheets in place.

If you want to change the z-index rather than removing it, simply replace the blank string '' with a new value or variable.

*Edit for my own sanity: Did away with parseInt() in favor of converting the returned z-index to a string (jQuery tries to return an integer); this way we can accommodate searches for values such as inherit.

Upvotes: 2

tymeJV
tymeJV

Reputation: 104785

You can use the filter function:

$("yourselector").filter(function() {
    return $(this).css("z-index") == 30; //change to your criteria
    //  OR
    //  return this.style.zIndex == 30;
}).css("z-index", ''); //your new zindex

Upvotes: 3

Related Questions