Jquery - selector made from object and string, can this be done?

Suppose there is an object passed as an argument to a function. The name of the argument is "obj". can it be combined as it should?

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

OR

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

Thank.

+3
source share
3 answers

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

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

Or, if you want to find children with such qualities:

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

Or an alternative syntax findthat gives objas a function context $():

$('.className', obj)...
$('[name=obj_name]', obj)...
+10
source

The second argument to your selector is the context:

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

obj. , , obj div.parent:

<div class="parent">
  <p class="className">I'll be found</p>
</div>
<p class="className">I will NOT be found</p>
+4
$(obj.tagName + " .className")
0

Source: https://habr.com/ru/post/1727119/


All Articles