2

Is there any way to get the selector for a jquery object

e.g in firefox I see a jquery object as [p.basket]

but there seems to be no way in jquery that I can get this selector?

Is there any way?

Phil

1

2 Answers 2

2

If a jQuery object was created with a selector string, then you can just look at its "selector" property. However, not all jQuery objects are so constructed. Thus you should make sure to check for null.

edit — if your jQuery object was not constructed with a selector, then there simply is not a selector available. The library does not have any built-in way of creating a selector that matches the set of elements it contains. You could do that yourself, though it's not clear why it would be useful; once you have a reference to the DOM elements (which you do if the jQuery object isn't empty), isn't that more useful?

Sign up to request clarification or add additional context in comments.

2 Comments

"not all jQuery objects are so constructed", can you elaborate that? Sounds very interesting, I do not know what queries do have this behaviour or not.
@Caspar Kleijne well for example if you already have a reference to a DOM element, and you pass it to jQuery(), then you get a jQuery object wrapped around that element, and there is no selector.
1

Is the selector property what you want?

1 Comment

nope, i found the selector property and that doesn't work as it was not constructed that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.