6

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.

Nope
  • 22,147
  • 7
  • 47
  • 72
  • 2
    This is a private (as in "not documented in the API") property. You understand you're asking us to explain the internal implementation of a library ? – Denys Séguret Sep 14 '12 at 14:36
  • 2
    The reason you can't find `.selector` in the API documentation is probably because it was intended for internal use only. You wouldn't be expected to use it at all, and you won't find it documented anywhere official. – Blazemonger Sep 14 '12 at 14:41
  • @dystroy: Thanks for the reply. I wasn't aware of `.selector` being private. If it was only intended for internal use I more than happily stay away from it :) No need to explain internal implementations to me. I would have loved to see any documentation on it though. where do I find that information? – Nope Sep 14 '12 at 14:42
  • I don't think using .selector for your own logic is supported, but here is another question where .selector discussed. http://stackoverflow.com/questions/2420970/how-can-i-get-selector-from-jquery-object – jacobq Sep 14 '12 at 14:43
  • 1
    You might also want to read more about Sizzle (selector engine used by jQuery) if you're curious: http://blog.jquery.com/2012/07/04/the-new-sizzle/ – jacobq Sep 14 '12 at 14:45
  • Thank you for all the info and links. +1 to that. I'm glad I found out about it now rather than later after missusing the API all over the place. – Nope Sep 14 '12 at 15:04
  • Not sure why votes are being casted to close this question as `off-topic`. I used the `$("#elementid").selector` property in my application, it did not do what I expected it to do. I asked why it is and luckily I got an answer before the question is closed which solved my software development and programming issue. I re-phrased my question in the post. Should be quite clear now that the issue was programming related. – Nope Sep 16 '12 at 00:39
  • 2
    I initially voted to close because API implementation is generally considered as not debatable on SO (should be debated with the owners of the code). Then I thought again that this was an accessible property and that jQuery isn't just the small component of a closed company but both one of the most used library and a very small (thus studyable) open source code. This is why I answered. I couldn't remove my close vote but I can now vote to reopen. The line between private implementation we can't discuss and this case is thin, thus the risk to have such problem. – Denys Séguret Sep 16 '12 at 08:02
  • @dystroy: Thanks for explaining this as I was a little surprised about the closing votes. I checked the FAQ as well and wasn't able to see how this question was not programming related. The way you explained it though I can at least see now why that was the case. I tried re-phrasing my question a little to make it more applicable. Regardless I'm glad you answered it and hopefully anyone else miss-using `.selector` will find this question useful. Thanks again for the reply. – Nope Sep 16 '12 at 08:32

1 Answers1

5

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • That is very interesting. Thank you for all the usefull information. I wasn't aware of the `if it is not documented it is internal or private.` I agree 100% on your last statement. It is to risky alright. I just wanted to make sure I didn't miss the documentation. I didn't use `.selector` before but when I noticed it today I thought it was something I can use. Which is clear now, no it is not :) I will remember what you said about the documentation. Thanks a lot. – Nope Sep 14 '12 at 15:13
  • They now have a section called *internals*, which I think means private APIs. Here's the [sparse] documentation on [`.selector`](http://api.jquery.com/selector/). – Joseph Silber Jan 31 '13 at 19:50