views:

3160

answers:

5

I have a jQuery selector, which has a chained function.

Inside the function I want to get access to the TEXT representing the expression for the selector.

$("cat dog").function() {

    // how do I get access to the "cat dog" string from inside THIS function ?
};

I've over simplified in this code sample what I actually want to do. I'm writing a plug-in and I need access to the selector for which the wrapped set has been created. Obviously in this particular example I have access to "cat dog" becasue i wrote it. So just picture this being in a plugin.

Its a little tricky to google for this.

+1  A: 

If you're using firebug you could console.log(this) inside the function and see if the selector string is accessible somewhere in the object. Sorry I am not familiar with the jQuery API.

Luca Matteis
i cant seem to immediately see it in there, but +1 for telling me something i didnt know. i was using IE and debugging in Visual Studio and not getting much useful by inspecting the object there
Simon_Weaver
+1  A: 

There is a 'selector' attribute in the jQuery object, but I'm not sure it's always available.

Ryan Doherty
i couldn't seem to access it when i tried. i'll wait a little longer to see if someone else is kind enough to try it for me :)
Simon_Weaver
its new in 1.3.1 If your using 1.2.6 it wont be available
redsquare
I just used this in 1.3.1.
Nosredna
oh great. thanks i'll check it out. i'm not switching to jQuery until theres an official 1.3.2 release due to this http://stackoverflow.com/questions/477463 (incidentally the post thats got me the most points so far!)
Simon_Weaver
using 1.3.2 now and this works as desired
Simon_Weaver
is the 'selector' a documented property? if its an undocumented property, i would not rely on it. i cant find any reference to it in the jquery docs.
Chii
You're right, it doesn't work all the time, especially if you are dynamically creating elements and want to give a back reference to the object/element that created the child.
Richard Clayton
A: 

well, I'm still trying to figure out if you are trying to get the element name? or the id and/or class or all of it.

You can get element with this

var element =$(this).get(0);
var id = $(this).attr('id');
var class=$(this).attr('class');

not sure what happens if you have more than one class. might go into an array or something, and you could get that with the indexes.

pedalpete
@pedalpete - i want that actual text "cat dog" - as a string
Simon_Weaver
@pedalpete - i think maybe the title of this post is a little misleading, but i hoped the question itself was clearer. little ambiguous looking at it. what I want is the actual string value that the $('cat dog') selector was constructed with.
Simon_Weaver
ha. thats really quite clever. unfortunately i DO need (more often than not in fact) to know when the selector returned zero items. i'm trying to construct more useful error messages for my assertion plugin http://stackoverflow.com/questions/498469
Simon_Weaver
sorry - i actually posted that last comment on the wrong answer. this wasnt what i was looking for
Simon_Weaver
+4  A: 

This is far from optimal but works in some cases. You could do the following:

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

This will return the last selector used for the element. But it will not work on non existing elements.

<div id='foo'>Select me!</div>
<script type='text/javascript'>
 $('#foo').getSelector(); //'#foo'
 $('div[id="foo"]').getSelector(); //'div[id="foo"]'
 $('#iDoNotExist').getSelector(); // undefined
</script>

This works with jQuery 1.2.6 and 1.3.1 and possibly other versions.

Also:

<div id='foo'>Select me!</div>
<script type='text/javascript'>
 $foo = $('div#foo');
 $('#foo').getSelector(); //'#foo'
 $foo.getSelector(); //'#foo' instead of 'div#foo'
</script>

Edit
If you check immidiatly after the selector has been used you could use the following in your plugin:

jQuery.getLastSelector = function() {
 return jQuery.getLastSelector.lastSelector;
};
jQuery.fn._init = jQuery.fn.init
jQuery.fn.init = function( selector, context ) {
 if(typeof selector === 'string') {
  jQuery.getLastSelector.lastSelector = selector;
 }
 return jQuery.fn._init( selector, context );
};

Then the following would work:

<div id='foo'>Select me!</div>
<script type='text/javascript'>
 $('div#foo');
 $.getLastSelector(); //'#foo'
 $('#iDoNotExist');
 $.getLastSelector(); // #iDoNotExist'
</script>

In your plugin you could do:

jQuery.fn.myPlugin = function(){
 selector = $.getLastSelector;
 alert(selector);
 this.each( function() {
  //do plugins stuff
 }
}

$('div').myPlugin(); //alerts 'div'
$('#iDoNotExist').myPlugin(); //alerts '#iDoNotExist'

But still:

$div = $('div');
$('foo');
$div.myPlugin(); //alerts 'foo'
Pim Jager
ha. thats really quite clever. unfortunately i DO need (more often than not in fact) to know when the selector returned zero items. i'm trying to construct more useful error messages for my assertion plugin http://stackoverflow.com/questions/498469 – Simon
Simon_Weaver
i'm not awake enough to re-read this just now. i only just saw you'd updated this. just wanted to raise attention to anyone using this code that if they see some wierdness occuring it could be due to this problem : http://stackoverflow.com/questions/535967/
Simon_Weaver
A: 

I couldn't figure out how to get the reference to the string value of the selector provided to inside $('value_i_want_here') when bound to a function; seems like a tough problem. However, if you prefer testing in IE[x] there's always Firebug Lite which works really well utilizing console.log(var|val).

David Mosher