1

I have the following html. How would I change the css for inner div that is within the outer div with id 'outerDiv1' and a class called Class1 is applied to outer div? In the page, there are multiple such outerdiv/innerdiv combinations, but in each case the outer div has a different id.

HTML

<div class="Class1 Class2" id="outerDiv1" >
   <div class="raDiv" style="position: fixed; background-position: 420px 427px;></div>
</div>

<div class="Class1 Class2" id="outerDiv2" >
   <div class="raDiv" style="position: fixed; background-position: 420px 427px;></div>
</div>

I tried the following jQuery code, but it doesn't seem to work.

jQuery to change a specific inner div CSS

$("#outerDiv1 .Class1 .raDiv").css("backgroundPosition", "420px 600px");
1
  • just remove the .Class1 as it is not inside #outerDiv1 or remove the space between #outerDiv1 and .Class1 as #outerDiv1.Class1. Also make sure you are doing things after document.ready Commented Apr 17, 2014 at 4:43

6 Answers 6

3

The ID of element is supposed to be unique so do not use the class selector with outerDiv1

Use descendant selector most suitable when child could be n level down in hierarchy.

$("#outerDiv1 .raDiv").css("backgroundPosition", "420px 600px");

Or As .raDiv is direct child of #outerDiv1 you can use parent-child selector

$("#outerDiv1 > .raDiv").css("backgroundPosition", "420px 600px");

Edit The problem with the selector keeping aside if this is good approach or not, is the space between id and class as space will make it descendant selector and look for .Class1 in descendants. Your could will work by removing space between id outerDiv1 and class Class1

$("#outerDiv1.Class1 .raDiv").css("backgroundPosition", "420px 600px");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. But I found that by excluding a blank between id and class at same level, it works. So this works: $("#outerDiv1.Class1 .raDiv").css("backgroundPosition", "420px 600px"). The only difference is I have removed the blank between the id and class for the same level element.
Yes you can combine id and class selector like that. I would not use that as when I can get the element with id I wont another selection criteria of class. That is why I told you to use only id selector without class selector.
You mean your first and second solutions will be faster than the third approach?
1

You can use from parent div -->

#outerDiv1 .raDiv {
  background-position:420px 600px;
} 


#outerDiv2 .raDiv {
  background-position: 0 0;
} 

Using Jquery ;

$("#outerDiv1 .raDiv").css("backgroundPosition", "420px 600px");

USER ISSUE. //$("#outerDiv1 .Class1 .raDiv") - This means

<div id="outerDiv1">
  <div class="class1">
    <div class="raDiv"></div>
  </div>
</div>

Comments

1

Agree with Adil's Answer you can also do with this also.

$('#outerDiv1').find('.raDiv').css("backgroundPosition", "420px 600px");

OR

$('#outerDiv1 > raDiv').css("backgroundPosition", "420px 600px");

OR

$('#outerDiv1').children().css("backgroundPosition", "420px 600px");

Comments

1

Three ways to do that:-

1) $("#outerDiv1.Class1 .raDiv").css("backgroundPosition", "420px 600px");

2) $("#outerDiv1 .raDiv").css("backgroundPosition", "420px 600px");

3) $("#outerDiv1 > .raDiv").css("backgroundPosition", "420px 600px");

Comments

0

If you want a generic solution for the first child div of the outerDivs, try this:

$('#outerDiv1 > div').css("backgroundPosition", "420px 600px");

Comments

0

no blank in each id and classes, and blank for parent descendant

$("#outerDiv1.Class1 .raDiv").css("backgroundPosition", "420px 600px");
       //no blank  //a blank

$("#outerDiv1.Class1) // a element with #outerDiv1 and .Class1
$("#outerDiv1.Class1 .raDiv) // a element with .raDiv descendant under #outerDiv1.Class1

2 Comments

Could you explain or provide a link for 'blank for parent descendant'? Does it mean that the Class1 would be a child of outerDiv1 if a blank is included?
Excellent. I hope I could give you a million points. You told me something great. Following your rule for excluding a blank if at the same level, then it works perfectly. This works: $("#outerDiv1.Class1 .raDiv").css("backgroundPosition", "420px 600px")

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.