JQuery: get source selector

I am in the middle of writing a plugin, and I would like to get the original selector that jQuery used to create the object.

So, if you wanted to apply something like .siblings() , you could get all siblings of this type, regardless of whether they are looking for siblings of a particular class or siblings of a particular type of element.

  • jQuery('div') - 'div'
  • jQuery(jQuery('div')) - object [jQuery] // requires a recursive search for the selector of this
  • jQuery('#elment') - '#element'
  • jQuery('.class') - '.class'
+6
source share
2 answers

Just go into the jQuery selector property of the property:

 console.log($("div").selector); // 'div' console.log($("#foo").selector); // '#foo' 
+10
source

As a complement to what Karim did:

 var t = jQuery('.clName'); t.each(function(){ jQuery(this).data('selector',t.selector); }); 
+2
source

Source: https://habr.com/ru/post/896022/


All Articles