Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the .selector property in jQuery not store a valid selector value?

Tags:

jquery

Assuming I have HTML similar to this:

<div class="fooContainer">
    <div class="barContainer">
        <a href="#">foo-bar</a>
    </div>
</div>

If in script my entry point is the anchor and I need to get to the div with the class fooContainer I can do this:

var $fooContainer = $("a").parents(".fooContainer");

This works perfectly fine as $fooContainer now holds the reference to the element in the DOM.

When I now print out the selector value like this:

console.log($fooContainer.selector)

I get the value a.parents(.fooContainer).

I was under the impression that the selector property returned a string which itself would be a valid selector value.

This is not the case though as when trying to use it as a selector like this:

$("a.parents(.fooContainer)")

It cannot find a match in jQuery 1.7.2.
And in the latest jQuery 1.8.x it even throws an exception: Error: Syntax error, unrecognized expression: a.parents(.fooContainer)

  • Why does the selector property not contain a valid selector value?
  • If it doesn't what would one use the selector property for?

I tried searching the jQuery documentation for information but was not able to find anything related to that property.

like image 935
Nope Avatar asked Sep 14 '12 14:09

Nope


1 Answers

You can easily read the source code but what isn't documented in the browsable API is internal and private.

By definition.

So this is an implementation detail, and a property you really shouldn't try to use as there is no guarantee, neither for the future versions nor for any use you might imagine today.

like image 188
Denys Séguret Avatar answered Nov 15 '22 10:11

Denys Séguret