6

As of jQuery 1.9 the .selector property of jQuery objects has been removed. (I'm a little confused as to why, exactly). I actually use it in a few unique scenarios, and I know that I could do other things to prevent this. Just wondering if anyone knows another way of grabbing the selector as of 1.9?

$('#whatever').selector // (would of returned '#whatever')  

One example of where I need .selector is when I already have a group of checkboxes by name, and I want to see, within that group, which one is checked:

jsFiddle DEMO

var $test = $('input[name="test"]');

console.log( $test );
console.log( $(':checked', $test).attr('id') ); // returns --undefined--

console.log( 'now with .selector: ');
console.log( $($test.selector + ':checked').attr('id') ); // returns correct

From the docs: .selector property on jQuery objects

The remaining purpose of the deprecated .selector property on a jQuery object is to support the deprecated .live() event. In 1.9, jQuery no longer attempts to maintain this property in chained methods, since the use of chained methods was never supported with .live(). Do not use the .selector property on a jQuery object. The jQuery Migrate plugin does not attempt to maintain this property.

Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96
  • 5
    mind providing one of those few unique scenarios? (code, not description, please) – PlantTheIdea Feb 19 '13 at 18:29
  • 2
    `$('#whatever').selector` still seems to work. The documentation says *"In 1.9, jQuery no longer attempts to maintain this property **in chained methods** [...]"*. Though http://api.jquery.com/selector/ claims it was removed. I don't know, it's a bit confusing. I guess an official statement might clarify this, maybe you can post in their mailing list/forum/group/whatever. – Felix Kling Feb 19 '13 at 18:30
  • 1
    Why not store the selector seperately? – marsze Feb 19 '13 at 18:32
  • 3
    Uhm, in your use case, that's what `.filter` is for: http://api.jquery.com/filter/. `$test.filter(':checked').attr('id')`. `$(':checked', $test)` cannot work, because `input` elements don't have any descendants. – Felix Kling Feb 19 '13 at 18:37
  • @FelixKling damn it that does work better :/ Well shit I guess that's an easy enough alternative. Welp, that's embarrassing :) – Mark Pieszak - Trilon.io Feb 19 '13 at 18:38
  • 2
    Yeah, well, we always learn something new :) – Felix Kling Feb 19 '13 at 18:39
  • Just put your `.filter()` thing as an example for an answer. @FelixKling – Mark Pieszak - Trilon.io Feb 19 '13 at 18:42
  • To answer Felix Kling, the official statement is quoted there and seems pretty unambiguous: **Do not use the `.selector` property on a jQuery object.** – Dave Methvin Mar 28 '13 at 16:54
  • Possible duplicate of [jQuery .selector property removed, workaround?](https://stackoverflow.com/questions/16836763/jquery-selector-property-removed-workaround) – thdoan Jun 15 '17 at 03:00
  • This question was asked prior to that one – Mark Pieszak - Trilon.io Jun 15 '17 at 15:43

3 Answers3

4

There should not be many reasons to actually need the original selector. In your specific use case, if you want to narrow down the set of selected elements, you can use .filter [docs]:

$test.filter(':checked').attr('id')

$('#whatever').selector still seems to work though. The documentation says "In 1.9, jQuery no longer attempts to maintain this property in chained methods [...]". Though http://api.jquery.com/selector claims it was removed in 1.9. I don't know, it's a bit confusing.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

I would just save the selector in a variable like this:

var testSelector = 'input[name="test"]';
var test = $(testSelector);
console.log( $(testSelector + ':checked').attr('id') );
nivs1978
  • 1,126
  • 14
  • 20
0

For the code I was working on I was able to replace

if ($elem.selector == 'date-column-filter')){

with

if ($elem.hasClass('date-column-filter')){
Matt Doran
  • 2,050
  • 2
  • 16
  • 18