Is there a method in jQuery to select an element located at a particular position?
For example, can I select the element that is located at left:100 and top:300 in absolute position?
It would be nice if I could select an element located in a range of positions, for example, select the element that is located left: 100 - 150 px top 200 - 280px.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
The :eq() selector selects an element with a specific index number. The index numbers start at 0, so the first element will have the index number 0 (not 1). This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).
jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.
You are looking for the .elementFromPoint()
JavaScript/DOM method.
var elem = document.elementFromPoint(100, 100) // x, y
That returns a DOM node
, which of course then can be wrapped into a jQuery object:
$(elem).remove(); // for instance
I'm not that aware about the cross-browser compatibility and I would like some guys who know better to edit this post or write a comment about it.
Reference: .elementFromPoint()
Example Link: http://www.jsfiddle.net/YjC6y/22/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With