0

I have this:

jQuery('td:contains("Yesss")').closest('tr').css('background-color','#ddf8dd')

How do I correctly add z-index:200?

Thank you!

4 Answers 4

3

The easiest way is to pass an object to the .css() method, i.e.:

jQuery('td:contains("Yesss")').closest('tr').css({
    'background-color': '#ddf8dd',
    'z-index': 200
});

Note that the z-index has to be wrapped in single quotes, otherwise JS will interpret the - as a mathematical minus/subtraction operator. Of course, you can always write the keys using camelCase, i.e.:

jQuery('td:contains("Yesss")').closest('tr').css({
    backgroundColor: '#ddf8dd',
    zIndex: 200
});

If you want to assign the z-index in a separate line, you can do this:

jQuery('td:contains("Yesss")').closest('tr').css('background-color', '#ddf8dd');
jQuery('td:contains("Yesss")').closest('tr').css('z-index, 200);

Not very efficient or pretty though, there's a lot of bloat. You can cache the selector instead:

var $el = jQuery('td:contains("Yesss")').closest('tr');
$el.css('background-color', '#ddf8dd');

// Some lines later
$el.css('z-index', 200);
Sign up to request clarification or add additional context in comments.

2 Comments

looks like I need to keep this format -> .css('background-color','#ddf8dd')
@user3903810 Why so? There is no reason why you can't pass in a plain object. I did fix a small typo tho, my UK spellcheck is converting background-color to background-colour
0

The object key should be changed to zIndex as should your backgroundColor.. Any dashed names should be camelcased.

1 Comment

the way I set it works correctly, what it is not working is this: .css({ 'background-colour': '#ddf8dd', 'z-index': 200 .... this is not showing the background color correctly either.
0
$('td:contains("Yesss")').closest('tr').css({
 'background-color': 'red',
 'z-index':'200'
})

this will work if tr position absolute

Comments

0

$("#elementId").css("z-index", "100");

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.