JQuery - Is it possible to combine a reference to an object with other selectors?

Sometimes I pass an “object” in jQuery, and not in an identifier or other selectors. for instance

<input type="text" onclick="doit(this)" />

<script type="text/javascript">
function doit(e) {
    var a = $(e).val();
}

This is useful when identifiers are not convenient, for example, long lists of elements or dynamically created input fields. I really saw examples of passing an object. I just tried it and it works.

My question is: can I combine the object selector with other selectors, for example, with identifiers. therefore instead of:

$("#myDiv .myClass").something()

I would not know how to encode it - something like this could be:

$(e + " .myclass").something

thank

+3
source share
2 answers

Do it:

$(e).find(".myclass").something();

or a slightly shorter but less efficient version:

$(".myclass", e).something();

, +, , e DOM, , - :

"[object HTMLInputElement] .myclass"
+6

. jQuery.

http://api.jquery.com/jQuery/

$(".myclass", e).something();
+2

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


All Articles