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.
edit: the 'selector' property is unfortunately now deprecated. http://jquery.com/upgrade-guide/1.9/#selector-property-on-jquery-objects
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
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.
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).
This is far from optimal but works in some cases. You could do the following:
This will return the last selector used for the element. But it will not work on non existing elements.
This works with jQuery 1.2.6 and 1.3.1 and possibly other versions.
Also:
Edit
If you check immidiatly after the selector has been used you could use the following in your plugin:
Then the following would work:
In your plugin you could do:
But still:
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.Maybe this solve your problem :
This will work if you want to access selector string in your function:
This will also work if multiple selector are use,
for instance,