Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to find an element at a particular position?

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.

like image 917
Thomas John Avatar asked Oct 15 '10 13:10

Thomas John


People also ask

How do I target a specific element in jQuery?

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.

How do you select an element by index?

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).

What is offset () in jQuery?

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.


1 Answers

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/

like image 179
jAndy Avatar answered Oct 12 '22 06:10

jAndy