tags:

views:

112

answers:

4

lets say i have a tree of the following structure:

<div>node level1
     <div>node level2
          <div>node level3...etc

Is there a way in jQuery, that if div level1 is clicked, only divs of level2 respond?

meaning:

$('div').click(function(){
$('div').children(first div child).css('color','red');
})

thanks for any input!

::EDIT::
hey everyone, thanks for your patience and input. i stubbornly went about solving this problem, and it worked out. i just had to find a way to crack inside the structure of my tree and it all became very clear.

i basically wanted to find a way to make a superfish-esque tree collapse/expand without being stuck inside the "li ul li" examples i had been seeing as I felt they were a bit opaque.


this fiddle has my solution. note: i am assuming that if something has 0 children it will most likely be a link to be followed. i am sure with the structure I have set out, and your able minds will be able to solve this last step.

thanks for all help and i look forward to any feedback.
final note: this can be easily soft coded to agree with any tree structure size. a while loop that acknowledges children or node clicked, that allows for concatenation and calls an eval function will be able to drill all the way down in the sub-nodes and make sure they are all hidden before the main node is also hidden.

just a side thought.

+3  A: 

You can do it like this:

$('div').click(function(){
  $(this).children('div').css('color','red');
});

Instead of selecting all <div> elements again, use this and do .children() from there to get the immediate child <div> elements. Here's a quick example, note for this example, the <div>s need a color initially, otherwise it'll cascade down...even though it wasn't actually applied to the level 3 elements.

Nick Craver
nickthank you for the response. let me specify my question with an example as well.<html><head> <style> div { color: black; } </style> <script type="text/javascript" src="jquery.js"></script></head> <body> <div>node level1 <div>node level2 <div>node level3...etc</div> </div> </div> </body></html><script>$(document).ready(function(){//css('color','red');$('div').click(function(){ $(this).find(":first-child").toggle(); });});</script>demonstrates what i am attempting to do.
jason m
@user360826 - I'm not following...`.find()` will find `:first-child` at **each** level, which includes level 2 and 3, if you don't want level 3 you need `.children()` instead of `.find()`.
Nick Craver
+1  A: 

This is probably what you're looking for:

http://api.jquery.com/first-child-selector/

Java Drinker
A: 

Edited for .click() context:

Inside the click event you'll have the this jQuery object to work with (which is the element which has been clicked. So you can use the .children() to access all of the children elements (it will only return immediate children, so just items level-2 items in your scenario):

$('div').click(function(){
    $(this).children().css('color','red');
}

Or, if you only want the first level-2 item you could use the :first-child selector:

$('div').click(function(){
    $(':first-child', this).css('color','red');
}
STW
yooder, thank you for the input. if you have a moment please look at the code i placed in Nick's response. it is exactly what i am trying to do.
jason m
@user360826 if your example is exactly what you want then it should be your solution--however the `.find()` call works with multiple levels of descendent/nested elements--I would really recommend spending sometime with jQuery's API and examining the differences between `.children()` and `.find()`-- they serve distinct purposes, and (as with all programming) it's important to understand their intended usages as well as their differences
STW
A: 

It's the matter of the css selector as jquery use it.

I think you could simply define a class to the div you want to select:

<div class="Level1">node level1
    <div class="Level2">node level2</div>
</div>

Then select it in jquery like this:

$('div.Level1').click(function(){
$(this).children('div.Level2').css('color','red');
})
Carson
adding a class just to use as a `.children()` selector is a bit redundant--if he's looking for all immeditate children of his level-1 div's then `.children()`, without a selector will give him what he's after without the extra (small) performance hit of running an extra seelctor
STW
My solution should be straight forward if he / she only want to select level 1 and level 2 element.performance depends on what he exactly want to do. his question is not clear actually.
Carson