javascript - Use document.elementFromPoint to check if an element at specific co-ordinates has a particular class -


i have coordinates. want check if there's element specific class @ these coordinates. how this?

i've tried following code (and here's jsfiddle: https://jsfiddle.net/0o160f5g/2/):

html:

<div class="foo"></div> 

css:

.foo{     width: 100px;     height: 100px;     background-color: red; } 

js:

if(document.elementfrompoint(30, 30) == ".foo"){     alert("the red square within coordinates") }else{     alert("the red square outside coordinates") } 

this code should give alert "the red square within coordinates". gives alert "the red square outside coordinates". can fix it?

elementfrompoint returns element object (or null), not css selector. test various properties of element see if matched

//checks class document.elementfrompoint(30,30).classlist.contains('foo') //checks id document.elementfrompoint(30,30).id == 'foo' //test null first not type errors  var eleatpoint = document.elementfrompoint(30,30); if(eleatpoint && eleatpoint.classlist.contains("foo")){    //do something. } 

or if have element in mind compare reference have result of elementfrompoint

var elementtocheck = document.queryselector('.foo');  if(document.elementfrompoint(30,30) === elementtocheck){   //do something. } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -