tags:

views:

717

answers:

6

I am trying to apply a style to the inner div which is contained in the parent div. The parent div also contains tons of other div but I just want to apply the color to the [0] div.

$("#parentDiv div")[0].css('background-color', 'red');

For some reason it says "css function is not defined!"

+1  A: 

This should work

$("#parentDiv div:first").css(background-color, 'red');
That did not work either! Not sure what is wrong! Thanks!
azamsharp
The code I provided should find the first div tag inside an element with id parentDiv. If you meant to find a different element, please explain it more in depth so I can revise my answer. Otherwise make sure your HTML markup is correct.
A: 

Can't you use the "first-child" selector?

Example: $("#parentDiv div:first-child").css('background-color', 'red');

Zachary
A: 
$("#parentDiv div")[0] // returns a regular DOM object, NOT jquery extended

You might try:

$("#parentDiv div").eq(0).css('background-color', 'red');

Although, the :first selector should work.

If it still doesn't work, be sure to look at your HTML after the method has been run, to determine if the first element is really what you think it is.

TM
Awesome! thanks man!
azamsharp
This method will use a double the memory because you are turning a jquery object into another jquery object. Just so you know... In fact, just about every other suggestion on this page will be more efficient.. Nonetheless, it works, so, whatever...
KyleFarris
Thanks KyleFarris! I will check out other solutions posted on this page!
azamsharp
I agree, tghw's method is better. I have updated my answer (although I upvoted his as well). @KyleFarris, true, it was "double memory", but it's also true that it is a pretty incidental amount.
TM
@TM, Absolutely, I know... the extra memory usage is very insignificant. I just wanted to point out that there are *better* alternatives, however minor, (especially if they are very easy to switch to) that he can choose from. Good job, though.
KyleFarris
@TM Glad I could give you the right answer.As much memory as Firefox uses, plus that sort of code in a loop (which, by its nature, is rather likely), the double memory use is a problem. SO is supposed to give the right answer, let's not get lazy here.
tghw
@tghw, your method is better because it is clearer and cleaner. Actually reading the code, using eq() just uses slice(), which calls pushStack(), which in fact calls jQuery() again ANYWAY, which means it STILL creates a new jquery object anyway. So actually there isn't a memory issue. Check line 123 of the development version of jquery. Actually using the ":first" selector is what would avoid creating a new jquery object, but that won't always be better, for example if you plan to use other objects from the selection.
TM
+6  A: 

The jQuery way to do this is:

$('#parentDiv div').eq(0).css({backgroundColor: 'red'});

.eq(0) like [0] but gives you the jQuery object. JQuery's eq() docs explain most of it (except why it's "eq" and not something more sensible like "at").

tghw
agreed, this is better than the way i suggested. +1
TM
Good answer. I was trying to remember .eq(), but all I could come up with in my head was .get(), which I think has the same limitation as [0].
Nosredna
A: 

Sometimes I have better luck using the other CSS syntax. You can try this (it's also better if you are going to add multiple styles):

$('#parentDiv div:first').css({backgroundColor:'red'});

You can also try nthChild or eq selectors if you need to attach style(s) to some div other than the first (just remember nthChild is ONE-based, not ZERO-based...):

$('#parentDiv div:nthChild(1)').css({backgroundColor:'red'});
// or
$('#parentDiv div:eq(0)').css({backgroundColor:'red'});
KyleFarris
eq() is zero-based, isn't it?
Nosredna
Yes, eq is zero-based, even as part of the selector.
TM
Oh shoot... my bad, I got carried away. Indeed, only nthChild is one-based. I meant to put "(just remember nthChild is ONE-based...)". :-\ I'll fix it.
KyleFarris
A: 

I've run into a similar error like this and wasn't able to really figure out what was going wrong. It has to do with the function returning a DOM object vs. a jQuery object though.

In the end I did something like this, which worked, but I'm not sure it's the best way to do it:

var divID = $("#parentDiv").children("div").attr("id");
$("#"+divID).css('background-color', 'red');
paul