javascript - How to get the div in which the cursor or caret is placed? -


i have 3 contenteditabledivs , want know ,in div user has placed cursor or caret through tab key or mouse click,

    <div contenteditable="true"></div>     <div contenteditable="true"></div>     <div contenteditable="true"></div> 

can me out?

i worked out better solution ,this need enclosing div tags want check cursor parent div tag , using document.getselection() object can id of div in cursor or caret placed

    <!doctype html>     <html>     <head>     <meta charset=utf-8 />     <title>js bin</title>     </head>     <body>       <div  id=parent>             <div contenteditable="true" id=1>dddd             </div>             <div contenteditable="true" id=2>dddd             </div>             <div contenteditable="true" id=3>dddd             </div>       </div>       <input type="label" id="showres"></input>     </body>     <script type="text/javascript"> var div = document.getelementsbytagname('div');  var out=document.getelementbyid('parent');  function findcursor(){  var x=document.getselection(); var r=x.getrangeat(0); var tempnode=document.createelement("div"); tempnode.setattribute("id","cursorhook"); r.surroundcontents(tempnode); document.getelementbyid('showres').value=tempnode.parentnode.id; document.getelementbyid(tempnode.parentnode.id).removechild(document.getelementbyid("cursorhook"));   }   out.onkeyup=findcursor; out.onclick=findcursor;       </script>     </html> 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -