On user click I would like to get a list of all elements that resides at the clicked point.
For example, if user clicks on Hello here:
<div><span>Hello<span></div>
I would like to get the following list:
<span>element<div>element<body>element<html>element
What would be the easiest method to get these elements ?
EDIT: Based on clarification, I think this is what you mean:
EDIT: As pointed out by @Misha,
outerWidth()andouterHeight()should be used in lieu ofwidth()andheight()in order to get an accuraterange.Also, if there’s nothing to prevent event bubbling on the page, then the
clickshould be placed on thedocumentas it will be much more efficient. Even if some otherclickhandler prevents bubbling, you should still have theclickon thedocument, and just handle it separately from those handler that prevent bubbling.Example: http://jsfiddle.net/57bVR/3/
Original answer:
This will give you an Array of the tag names including the span. Couldn’t quite tell if this is what you wanted.
It uses
.parents()along with.andSelf()to get the elements, then uses.map()with.get()to create the Array.Example: http://jsfiddle.net/9cFTG/
If you just wanted the elements, not the tag names, get rid of
.map()and.get().Or if you wanted to join the Array into a String using some sort of separator, just add
.join(" ")after.get(), placing your separator inside the quotes.