Alternative to .selector property now that it is removed in jQuery 1.9
Asked Answered
S

3

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.

Scevor answered 19/2, 2013 at 18:28 Comment(10)
mind providing one of those few unique scenarios? (code, not description, please)Earhart
$('#whatever').selector still seems to work. The documentation says "In 1.9, jQuery no longer attempts to maintain this property in chained methods [...]". Though 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.Zug
Why not store the selector seperately?Displace
Uhm, in your use case, that's what .filter is for: api.jquery.com/filter. $test.filter(':checked').attr('id'). $(':checked', $test) cannot work, because input elements don't have any descendants.Zug
@FelixKling damn it that does work better :/ Well shit I guess that's an easy enough alternative. Welp, that's embarrassing :)Scevor
Yeah, well, we always learn something new :)Zug
Just put your .filter() thing as an example for an answer. @FelixKlingScevor
To answer Felix Kling, the official statement is quoted there and seems pretty unambiguous: Do not use the .selector property on a jQuery object.Thilda
Possible duplicate of jQuery .selector property removed, workaround?Marshamarshal
This question was asked prior to that oneScevor
Z
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.

Zug answered 19/2, 2013 at 18:44 Comment(1)
Thanks! Must of been having a brainfart... overreacted that it was gone hahaScevor
H
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') );
Hortense answered 17/4, 2020 at 9:5 Comment(0)
M
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')){
Myalgia answered 22/5, 2023 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.