1

I'd like to start that I know that there are a lot of question and answers to my question by I did not see one strong monovalent solution. My question is how to get JQuery selector string value that was depricated on 1.7 and removed (for my opinion with no reason) in 1.9: For example:

$image.parents('[class*=wrapper_zml]').selector \\returns '[class*=wrapper_zml]'
$('[id*=test]').selector \\returns '[id*=test]'
$image.parent().children('[src*="-icon-"]').selector \\ returns '[src*="-icon-"]'

I'd tried this solution from this answer:

jQuery.fn._init = jQuery.fn.init
jQuery.fn.init = function( selector, context ) {
    return (typeof selector === 'string') ? jQuery.fn._init(selector, context).data('selector', selector) : jQuery.fn._init( selector, context );
};

jQuery.fn.getSelector = function() {
    return jQuery(this).data('selector');
};

But in JQuery 2.1.4 it breaks append and prepend functions thus I got such console error:

TypeError: Cannot read property 'createDocumentFragment' of undefined at Function.jQuery.extend.buildFragment (chrome-extension://khehkclcpkokmaickfndjjalcoggbkme/js/jquery.js:5087:22) at jQuery.fn.extend.domManip (chrome-extension://khehkclcpkokmaickfndjjalcoggbkme/js/jquery.js:5387:22) at jQuery.fn.extend.append (chrome-extension://khehkclcpkokmaickfndjjalcoggbkme/js/jquery.js:5218:15)

Thanks for help guys!

Community
  • 1
  • 1
AlexBerd
  • 1,368
  • 2
  • 18
  • 39
  • 3
    This sounds like an XY problem. What are you trying to achieve by getting the selector? – Rory McCrossan Dec 21 '15 at 13:06
  • For example the selector that selected that object for further reuse or log. And why XY problem? Why not to open such functionality?, I really do not see problem with it – AlexBerd Dec 21 '15 at 13:12
  • They removed the functionality because there are better ways of achieving what you need. In this case, store the DOMElement (or jQuery object) referenced by `this` in a variable for later use. This is better than storing the string and selecting the element again as it saves accessing the DOM. – Rory McCrossan Dec 21 '15 at 13:13
  • Suppose you apply different methods to same element using different selectors still matching same element. What would suppose to be the relevant selector??? – A. Wolff Dec 21 '15 at 13:14
  • @RoryMcCrossan Agree with you, but such property is not harm to JQuery safety or anything else it just gives to user another tool for reuse. – AlexBerd Dec 21 '15 at 13:21
  • @A.Wolff It is simple, As I saw in 1.6 version, Jquery store selector value in data object, just store the array in ascending order. – AlexBerd Dec 21 '15 at 13:23
  • 1
    @AlexBerd I'm not sure to understand what you mean. This property has been removed because it was unreliable. Instead, you'd have better to include an example where you would need this property. – A. Wolff Dec 21 '15 at 13:37

0 Answers0