2

This works...

    var start = $('.nodes:first>span>div');
    var foo1 = $(".nodes:first>span>div>div>div>div>span>div");
    var foo2 = $(".nodes:first>span>div>div>div>div>span>div>div>div>div>span>div");
    var foo3 = $(".nodes:first>span>div>div>div>div>span>div>div>div>div>span>div>div>div>div>span>div");
    var foo4 = etc,etc...

Trying to consolidate to something like this...

    var start = $('.nodes:first>span>div');
    var separator = ">div>div>div>span>div";
    var foo1 = $("start + 1*separator");
    var foo2 = $("start + 2*separator");
    var foo3 = $("start + 3*separator");
    var foo4 = etc,etc...

Have been muddling for hours, but the syntax for this escapes me! Any pointers? Thanks!

Ryan Schultz
  • 307
  • 4
  • 17
  • possible duplicate of [How to use javascript variables in jquery selectors](http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors) – Phil Nov 16 '11 at 06:44
  • 1
    I'd rather ask whether there's a way to *simplify* these selectors/your HTML structure... – deceze Nov 16 '11 at 06:47

2 Answers2

4

The find() method seems like it should do it.

var $start = $('.nodes:first>span>div');
var separator = ">div>div>div>span>div";
var $foo1 = $start.find(separator);
var $foo2 = $foo1.find(separator); // etc...

If possible, I'd try and simplify your HTML so you don't need so many selectors though. This kind of code seems like it's going to be a nightmare to understand if you need to come back to it to fix something later.

Michael Low
  • 24,276
  • 16
  • 82
  • 119
3

Selectors are just strings so you just add strings to together and use them as a selector.

var start = '.nodes:first>span>div';
var separator = ">div>div>div>span>div";
var foo1 = $(start + separator);
var foo2 = $(start + separator + separator);
var foo3 = $(start + separator + separator + separator);

Though, if we understood what you were trying to accomplish, there is probably a much nicer way of doing it using less complication. For example, you should probably be using classes on the different types of divs and spans and then target specific classes without regard for how many intervening layers of divs there are. This makes your code much, much less brittle and much less tied to the exact HTML implementation (it still has some dependencies, but not near as many).

FYI, as far as I know, you can't multiply strings to get multiple copies of them so 2*str doesn't get you a string with two consecutive copies of str in it.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • thanks, jfriend00... what if you have a large number of foo's though (say 1000)? Wondering what the 'for statement' would look like? – Ryan Schultz Nov 16 '11 at 06:52
  • 5
    You do NOT want construct selectors with 1000s of items in them. It's just the wrong way to solve the problem and will probably execute very slowly. If you're thinking about needing that, then you're going about your problem the wrong way and it would be better for you to describe the actual problem (including sample HTML) and we'll offer you much better approaches. – jfriend00 Nov 16 '11 at 06:58
  • well, it's not a 1000, but fooX will constantly fluctuate. Looks like we're going to add some classes to the create a selectable tier structure...thanks for the suggestion! – Ryan Schultz Nov 16 '11 at 07:30