$("*").click(function(){
$(this); // how can I get selector from $(this) ?
});
Is there an easy way to get selector from $(this)
? There is a way to select an element by its selector, but what about getting the selector from element?
$("*").click(function(){
$(this); // how can I get selector from $(this) ?
});
Is there an easy way to get selector from $(this)
? There is a way to select an element by its selector, but what about getting the selector from element?
Are you trying to get the name of the current tag that was clicked?
If so, do this..
You can't really get the "selector", the "selector" in your case is
*
.http://www.selectorgadget.com/ is a bookmarklet designed explicitly for this use case.
That said, I agree with most other people in that you should just learn CSS selectors yourself, trying to generate them with code is not sustainable. :)
I was getting multiple elements even after above solutions, so i extended dds1024 work, for even more pin-pointing dom element.
e.g. DIV:nth-child(1) DIV:nth-child(3) DIV:nth-child(1) ARTICLE:nth-child(1) DIV:nth-child(1) DIV:nth-child(8) DIV:nth-child(2) DIV:nth-child(1) DIV:nth-child(2) DIV:nth-child(1) H4:nth-child(2)
Code:
Taking in account some answers read here I'd like to propose this:
Maybe useful to create a jQuery plugin?
The best answer would be
Just add a layer over the $ function this way:
Now you can do things like
and will return "a" even on newer jQuery versions.