AdamSchnaare
AdamSchnaare

Reputation: 35

How do I use the jQuery selector string in the function I call?

$('.someClass').someFunc();

someFunc(){
   var thatClass = **???**;
}

I'm wondering how to get the string that I used in the jQuery call into the Function that I chain to it. Is there even a way?

Upvotes: 0

Views: 68

Answers (2)

LeGEC
LeGEC

Reputation: 51810

As Anton said : you can look at the selector property.

However, your jQuery selection may have been built using traversing functions (.parent(), .eq(), etc... ), or directly selecting html nodes. In most of those cases, the jQuery object will have an empty selector string :

var $divs = $('div.mine');
console.log( $divs.selector ); //prints 'div.mine'
var $children = $divs.children();
console.log( $children.selector ); //prints '' (empty string)
var $oneDiv = $( $divs[0] );
console.log( $oneDiv.selector ); //prints '' (empty string)

Upvotes: 0

Anton
Anton

Reputation: 32581

You can use selector

var thatSelector = $(this).selector; //returns ".someclass"

Upvotes: 3

Related Questions