views:

129

answers:

3

Hello,

Assume that there is an object, passed as an argument to a function. the argument name is "obj". can it be concatenated as followed?

$(obj + " .className")......

OR

$(obj + "[name='obj_name'])......

Thanks.

+2  A: 

No, but you can use the filter() method to filter out the object itself:

$(obj).filter('.className')...
$(obj).filter('[name=obj_name]')...

Or, if you want to find the children with those qualities:

$(obj).find('.className')...
$(obj).find('[name=obj_name]')...

Or, an alternative syntax to find, giving the obj as the context to the $() function:

$('.className', obj)...
$('[name=obj_name]', obj)...
Tatu Ulmanen
I prefer the latter method.
Mark
thank you all, got my answers
Yossi
A: 
$(obj.tagName + " .className")
Paul Creasey
That doesn't work if the object is for example a children of some other object. That might return unexpected results.
Tatu Ulmanen
The question is ambiguous, i interpret it as obj being a instance of a DOM element, then using a selector to find any other elements of that type with a specified class. No where does he mention wanting to find children of obj, though that may indeed be the requirement.
Paul Creasey
+1  A: 

The second argument of your selector is the context:

$(".className", obj).each(...);

This will restrict all matches to obj. So assuming obj is a reference to div.parent:

<div class="parent">
  <p class="className">I'll be found</p>
</div>
<p class="className">I will NOT be found</p>
Jonathan Sampson