3

Is there any method in jQuery to select all element located at a particular position for example select the element that is located at left:100 and top:300 and another div is located at same position, on click I want to get ID's of both Div?

It would be nice if I could select and element located in a range of positions,

http://jsfiddle.net/simmi_simmi123/YjC6y/1908/

This Fiddle but its showing ID of only top div.

$(document).ready(function(){        
    $(document.body).bind('click', function(e){
        var elem = document.elementFromPoint(e.pageX, e.pageY);

        alert('you selected element id: ' + elem.id);
    });
});
Peter O.
  • 32,158
  • 14
  • 82
  • 96
anam
  • 3,905
  • 16
  • 45
  • 85
  • possible duplicate of [find elements that are stacked under (visually) an element in jquery](http://stackoverflow.com/questions/5598953/find-elements-that-are-stacked-under-visually-an-element-in-jquery) – andyb May 22 '13 at 14:03

2 Answers2

3

Try my fiddle http://jsfiddle.net/YjC6y/1911/

It uses the code from possible dup as commented on your original post:

$(document).ready(function(){        
    $(document.body).bind('click', function(e){
        var elems = GetAllElementsAt(e.pageX, e.pageY);

        elems.each(function() {
            alert($(this).attr('id'));
        });
    });
});

function GetAllElementsAt(x, y) {
    var $elements = $("body *").map(function() {
        var $this = $(this);
        var offset = $this.offset();
        var l = offset.left;
        var t = offset.top;
        var h = $this.height();
        var w = $this.width();

        var maxx = l + w;
        var maxy = t + h;

        return (y <= maxy && y >= t) && (x <= maxx && x >= l) ? $this : null;
    });

    return $elements;
}

Main credits @hunter for this post: find elements that are stacked under (visually) an element in jquery

Community
  • 1
  • 1
Yeronimo
  • 1,731
  • 15
  • 28
0

Something like this -

$('*').filter(function () {
    if ($(this).position().left === 100 && $(this).position().top === 300)
      return true;
    else
      return false;
}).each(function () {
    console.log($(this).attr('id'));
});

Demo --> http://jsfiddle.net/YjC6y/1910/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111