0

How can I write the below CSS selector in jQuery. My intention is to get this particular style to work in older IE browsers

CSS:

.classA .classB>.classC .classD {
   background-color: red;
}

The following jQuery code does not seem to be working

$('.classA .classB>.classC .classD').css('background-color':'red');

Here is sample html and jquery code to illustare what I am asking for http://jsfiddle.net/58T7C/

1
  • it's working fine FIDDLE.. some syntax error in ur code Commented Dec 27, 2013 at 2:47

3 Answers 3

4

it is a valid css statement for old IE versions(>= IE7) also.

Child Selector is supported from IE 7, Class Selector is supported even in IE6, so if you don't want to support IE6 then you can go with css itself no need for jQuery

but the jQuery version has a syntax error

$('.classA .classB > .classC .classD').css('background-color', 'red');
                                                             ^ `,` here
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the error is probably due to the OP's confusion about the object argument syntax that .css() also accepts.
3

http://api.jquery.com/css/

Use a ',' instead of ':' inside the .css call

Comments

2

You have a syntax error. You could put braces to create an object:

$('.classA .classB>.classC .classD').css({'background-color':'red'});

EDIT:

As @m59 points out, the .css() function can be called two ways. One way is with an object. That way allows multiple properties to be set at once. The other way is with two strings, like this:

$('.classA .classB>.classC .classD').css('background-color', 'red');

1 Comment

Or just use a comma as in the other answers. .css() accepts either.

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.