4

I need an attribute selector to match the first div element with an z-index and a value of 0. tTis is how i tried to get hands on the element,by using the css attribute selector syntax in jquery:

$("div[z-index='0']").first().doSomeJqueryStuff() ....

In my document there is an explicitly set z-index with a value of 0 on a div element, but the selector returns no result. Thanks for help!

1

6 Answers 6

12

You can apply attribute selector to inline style attribute (* means contains):

div[style*="z-index: 0"]
Sign up to request clarification or add additional context in comments.

2 Comments

Make this the most voted answer. Pure CSS is the way, JQuery is unnecessary overhead in this case. Thanks Suraj.
Nice. However, note that this only works if the z-index: 0 is specified using a style attribute. If the z-index rule is part of a ruleset, the element won't be selected.
5

Try

$('div').each(function({ 
    if ($('div').css('z-index') === '0') { // or ... === 0
        ....
    }
}

Comments

1

You use Sizzle CSS Selector Library.

Comments

1

No that's not possible. You can not select elemets based on css attribute values that way.

You should apply a class or an id to an element with your CSS value and use that instead or iterate over all elements and check for presence of css attribute (rather costly operation).

Comments

1

You can check them one by one untile you found the first with z-index = 0 :

$('div').each(function({ 
    // Check doc about .css() function
});

Comments

1

z-index is a CSS property and 0 its value.

CSS selectors are made of HTML (and SVG, ...) id, class, elements, attributes, pseudo and their values (and Sizzle, the selector part of jQuery is made the same way). These CSS selectors are for CSS rules where CSS declarations (property: value) are written and will style the HTML document.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.