Jose Eduardo
Jose Eduardo

Reputation: 487

Get Element by Position determined in Z-Index - Javascript

I am using the following sentence to get the element in some position, but he takes the first ...

$(this.cTaskItem[0]).mouseup(function(event){
    var posX = event.clientX, posY = event.clientY;
    var overElem = document.elementFromPoint(posX, posY);
    overElem.style.border = "3px solid red";
});

I wonder how do I get the element at a given position and Z-Index.

Thank You

Upvotes: 1

Views: 2150

Answers (2)

OSdave
OSdave

Reputation: 8585

If the first element (the one that's selected by document.elementFromPoint(posX, posY)) is not supposed to be clickable you can set the css property pointer-events: none; to it and it will not be selected anymore

Upvotes: 2

Hayko Koryun
Hayko Koryun

Reputation: 1244

As @t.niese suggested above you could do this:

$(this.cTaskItem[0]).mouseup(function(event)
{
    var posX = event.clientX, posY = event.clientY;
    var elements = [];
    var elm = document.elementFromPoint(posX, posY);
    while(elm.tagName != "HTML")
    {
        elements.push(elm);
        elm.style.display = "none";
        elm = document.elementFromPoint(posX, posY);
    }

});

Then all you would need to do is go through your elements array and select the one you need.

Upvotes: 0

Related Questions