javascript - document.elementFromPoint returns null for a selected text -


i using following code position of selected text :

var text = window.getselection();  start = text.anchoroffset;  end = text.focusoffset - text.anchoroffset; 

and want selected text later .. using :

document.elementfrompoint(start,end); 

however, returns "null".

help please :)

elementfrompoint(start,end); requires xy page coordinates, passing index within string.

to xy page coordinates can use calculating xy-position of text selection . note coordinates viewport, may have account scrolling if want distance top of html document.

getselection().getrangeat(0).getclientrects()[0]; // clientrect {} // bottom: 226 // height: 15 // left: 300.75 // right: 305.078125     // top: 211 

width: 4.328125

to html element wraps text node, can use get parent element of selected text

window.getselection().anchornode.parentelement 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -