0

What I want to do is select an element by id, find one of its parents, and finally select a different child of that parent. I can already do that like this:

$('#id').parents('.class1').find('.class2');

However, I need to be able to do this using a single selector. For example:

$('#id parents .class1 .class2');

Is there an equivalent to the parents() method using just selectors?

Reyan
  • 584
  • 1
  • 4
  • 16
  • Are you trying to select sibling of a current selector? if not you can find the answer here http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector – hemanth Jan 29 '14 at 00:01
  • 1
    Why? I'm quite curious. – isherwood Jan 29 '14 at 00:03
  • Well, long story short I have a widget guy that takes a single selector as an input parameter; that wasn't sufficient to select something, so I may need to restructure this widget. – Reyan Jan 29 '14 at 00:52
  • And you can't set your el to a var? – isherwood Jan 29 '14 at 00:57

1 Answers1

2

Your question is really about CSS at this point, and parent selectors aren't available in CSS.

Based on your comment above, why not set your element to a variable?

var myEl = $('#id').parents('.class1').find('.class2');

widget(myEl);
isherwood
  • 46,000
  • 15
  • 100
  • 132
  • I was afraid that was the case. Thanks! – Reyan Jan 29 '14 at 00:51
  • Thanks for your suggestion. Its possible that that may work, although I'll have to think about it. I'm in a (perhaps) unusual situation where the people using the widget are not supposed to need to write any javascript. In the meantime, I've added some id's to the html I'm working with so I can select my stuff with a single selector ;) – Reyan Feb 04 '14 at 20:27