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

Eli
  • 14,779
  • 5
  • 59
  • 77
Phil Whittaker
  • 434
  • 4
  • 20
  • Possible duplicate of: **[How can i get selector from jQuery object](http://stackoverflow.com/questions/2420970)** – hippietrail Oct 17 '12 at 10:19

2 Answers2

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?

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • "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 Jan 13 '11 at 13:27
  • 2
    @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. – Pointy Jan 13 '11 at 13:39
1

Is the selector property what you want?

Steve
  • 15,606
  • 3
  • 44
  • 39