MrBrightside
MrBrightside

Reputation: 623

Two context objects in a single jQuery selector

I am wondering if there is a way to do give a jQuery selector two objects for context. The following snippet works but lacks a certain finesse in its syntax.

 jQ('#some_id_or_selector', first_jQ_obj.add(second_jQ_obj) )

Upvotes: 0

Views: 186

Answers (3)

Jay Blanchard
Jay Blanchard

Reputation: 34416

You can do this -

jQ('selector, selector, selector').doStuff(....

You can also use heirarchy -

jQ('parent', 'descnedants').doStuff(...

(corrected comma placement)

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Try using comma separated context like below,

$('someselector', 'context1, context2')

or Simply,

$('context1, context2').find('someselector') 

DEMO: http://jsfiddle.net/ud4MU/ and http://jsfiddle.net/ud4MU/1/

Note: If it is some ID in your selector, then you can select the element by ID as $('#some_id') instead of using any context.

Since you have jQuery objects you can do like below,

$context1.add($context2).find('selector');

DEMO: http://jsfiddle.net/ud4MU/3/

Upvotes: 3

Sushanth --
Sushanth --

Reputation: 55750

How about .find() function

first_jQ_obj.add(second_jQ_obj).find('#some_id_or_selector' )

But the ID in the HTML page should be unique..

So

jQ('#some_id_or_selector') should be sufficient

Upvotes: 1

Related Questions