tags:

views:

1560

answers:

5

I feel like I have to use way too many .children() in some of my jQuery functions.

alt text

Here's my HTML:

<div class="goal-small-container">
  <div class="goal-content">
    <div class="goal-row">
      <span class="goal-actions">

And here's my jQuery:

$('.goal-small-container').hover(function() {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "hidden"});
});

Is there a better way? Tell me for the kids.

+11  A: 

have you heard about .find() ?

$('.goal-small-container').hover(function() {
  $(this).find('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).find('.goal-actions').css({visibility: "hidden"});
});
Reigel
I had not! That is *exactly* what I was looking for. Also appreciate what I think was an edit.
Josh Smith
^_^ I was the first to answer, sadly, they got a higher votes. ;)
Reigel
I did appreciate that you were first to answer, but seeing `.find()` used with the chain of inherited classes and the single class was also useful for someone unfamiliar.
Josh Smith
no problem at all. ;)
Reigel
+8  A: 

Instead of

$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});

You can use:

$(this).children('.goal-content > .goal-row > .goal-actions').css({visibility: "visible"});

For exactly the same meaning. If there's no chance of that being ambiguous, however, (.goal-actions will only appear in that structure of the markup) you can just use find.

Ian Henry
+24  A: 

.find('.goal-content .goal-row .goal-action').whatever()

or more simply:

.find('.goal-action').whatever()

meder
This is not what I was expecting.
Josh Smith
@rockinthesixstring - party pooper
meder
lol hate answers with none-related images... I'm so tempted to down-vote.. ;)
Reigel
meder - The MJ was just creepy! ;o)
patrick dw
sorry. It was funny.. just not constructive.
rockinthesixstring
@patrick dw - Still made me giggle. :)
Gert G
For those late to the party, Michael Jackson is apparently unrelated to children.
Josh Smith
@rockinthesixstring If it generated page-views and more people learned, it was constructive.
Josh Smith
@Josh Smith - People can still see the image if they view the edits.
Gert G
@Gert - Yeah, it was (sadly) pretty funny. :o)
patrick dw
Next time, I'll bring some mob to vote my answer. Hahaha
Reigel
Instead of *use jQuery*, it can now be referred to as *use Micheal Jackson*.
alex
@alex, or maybe mjQueery
rockinthesixstring
@rockinthesixstring You say that, but *don't* want the MJ image... :P
alex
it's true. it was amusing for sure. but what did it have to do with the answer? (yes, I get the mj/kids analogy).
rockinthesixstring
@rockinthesixstring Well, let's see if it can survive *once* more. :D
alex
@alex - turd ;-)
rockinthesixstring
@alex I wish I could up-vote your edit. Your comment will do, good sir.
Josh Smith
looks like [Reigel](http://stackoverflow.com/users/251986/reigel) has a grudge, or a bone, or something.
rockinthesixstring
I would vote to rollback if the image came *after* the answer
Josh Stodola
The image was an edit that came after the original answer.
rockinthesixstring
+1 @The Other Josh
Josh Smith
@rockin - I think Josh Stodola meant *after* in the sense of sequential order in the post, not chronological order.
patrick dw
Make it even simpler by changing `$(this).find('.goal-action')` to `$('.goal-action', this)`.
shadowhand
@shadowhand - that syntax is not recommended per resig, plus `.find` is just easily more readable.
meder
@meder Do you have reference for that?
shadowhand
@shadowhand I'm pretty sure that context param (2nd argument) just gets translated to `find()` anyway.
alex
A: 

Why don't you just use .show() and .hide() on the second <div>? And, initially have them display hidden or something.

kchau
`.show()` and `.hide()` have been tested and *do not* have the desired effect.
Josh Smith
+1  A: 

You can just use:

$('.goal-small-container').hover(function() {
   $(this).find('goal-actions').show();
}, function() {
   $(this).find('goal-actions').hide();
});
mkoistinen
haha, you beat me to it.
rockinthesixstring
Dang. In the time it took me to write this on my iPhone, so many other answers came in!
mkoistinen
@mkoistinen Programming on the iPhone is a painful experience, IMO.
alex
Actually, `.show()` and `.hide()` have been tested and *do not* have the desired effect.
Josh Smith