[Solved] How do I retrieve the actual string selector


Answer for updated question:

How do I retrieve the actual “select#mysel” from the var mysel?

example of how this might be used?

var mysel = $("select#mysel");
var myopt = $(mysel.actualstringselection + " option");

So basically, you want the original selector string that you passed into $() in order to get mysel.

You can’t get that. jQuery never offered an official way to do it. For a long time, there was an undocumented property on jQuery instances that contained it (mostly, most of the time), but it was always undocumented because it wasn’t entirely reliable, and it doesn’t even exist in current versions.

So instead, you can do it yourself:

 var selector = "select#mysel";
 var mysel = $(selector).data("selector", selector);
 // Presumably somewhere else where you no longer have the `selector` variable:
 var myopt = $(mysel.data("selector") + " option");

But, you wouldn’t want to use that for the above. (You’d use find, as described originally [below]). The only reason I can think of for such a thing is if you’ve changed the DOM and want to repeat the original query. E.g.:

mysel = $(mysel.data("selector")); // Goes looking for the elements again

Original answer:

If after this line:

var mysel = $("select#mysel");

…you’re trying to use mysel to find something within the descendant elements of any DOM elements in that jQuery object, you use find:

var optionElements = mysel.find("option");

7

solved How do I retrieve the actual string selector