M
M
Maxim2011-12-09 13:21:02
JavaScript
Maxim, 2011-12-09 13:21:02

Node selection with jQuery?

There is a structure like this:

<br>
<table><br>
<tr><br>
<td class="tree-node">...</td><br>
</tr><br>
<tr><br>
<td class="tree-sub-node">...</td><br>
</tr><br>
<tr><br>
<td class="tree-sub-node">...</td><br>
</tr><br>
<tr><br>
<td class="tree-sub-node">...</td><br>
</tr><br>
<tr><br>
<td class="tree-node">...</td><br>
</tr><br>
<tr><br>
<td class="tree-sub-node">...</td><br>
</tr><br>
</table><br>

Obviously, someone pushed the tree into the table :)
I need to create a tree object according to this code.
How to use jquery in one line (well, or with a minimum of code) to select elements with a tree-sub-node class that are between a specific tree-node and up to the next element with a tree-node class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2011-12-09
@taliban

The simplest example:

$node = $('.tree-node:first');
$subnodes = [];
while( $node = $node->next('.tree-sub-node') )
{
    $subnodes.push( $node );
}

S
sdevalex, 2011-12-09
@sdevalex

I only have one idea for this...

$(function(){
    var i = 0;
    
    $('td').each(function(){
        $(this).hasClass('tree-sub-node') || ++i;
        $(this).attr('data-index', i);
    });
    
    $('td.tree-sub-node[data-index=2]').css('background', '#f00');
});

jsfiddle.net/7jpG4/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question