0

What I'm trying to do here is this:

function newFunction( element ){
    var newSelector = element.getSelector();
}

$('.class li a').on('click', function(){
    newFunction( $(this) );
})

There is no getSelector function, so how can I do this?

I thought about getting first the html tag of $(this) and then its parent and so on but I cannot find a way to get the html tag (not content) of $(this).

Any ideas?

Thanks

  • What are your trying to do? – Satpal Jan 29 '14 at 16:24
  • Am I? And how to I get it into a variable? `$(this)` is the whole element, no the selector. – Raúl De Zamacona Jan 29 '14 at 16:24
  • 1
    I guess you want to get `'.class li a'`, Might help [How can i get selector from jQuery object](http://stackoverflow.com/questions/2420970/how-can-i-get-selector-from-jquery-object) – Satpal Jan 29 '14 at 16:26
  • @Satpal yes, that's what I want, but I want to run the function no matter what I'm clicking on, so I want `.class li a` and `.otherClass li` and `whateverElement div ul li`, do I explain myself? – Raúl De Zamacona Jan 29 '14 at 16:26

2 Answers2

0

You just want the selector property - there's no getter function...

function newFunction(element) {
    var newSelector = $(element).selector;
}

$('.class li a').on('click', function(){
    newFunction(this);
})

(I passed the html element into the function, but you can obviously do it the way you originally have it. I just have OCD about that :p )

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
-1

Just use the jQuery method prop():

function newFunction( element ){
    var newSelector = element.prop("tagName");
}

NOTE: This will return the element's tag name capitalized (e.g. for an img element, prop("tagName") will return IMG).

Nate Kibler
  • 652
  • 3
  • 14