Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>an ugly solution: </p> <p>for ie: use document.selection...</p> <p>for ff: use a pre behind textarea, paste text before cursor into it, put a marker html element after it (cursorPos), and get the cursor position via that marker element</p> <p>Notes: | code is ugly, sorry for that | pre and textarea font must be the same | opacity is utilized for visualization | there is no autocomplete, just a cursor following div here (as you type inside textarea) (modify it based on your need)</p> <pre><code>&lt;html&gt; &lt;style&gt; pre.studentCodeColor{ position:absolute; margin:0; padding:0; border:1px solid blue; z-index:2; } textarea.studentCode{ position:relative; margin:0; padding:0; border:1px solid silver; z-index:3; overflow:visible; opacity:0.5; filter:alpha(opacity=50); } &lt;/style&gt; hello world&lt;br/&gt; how are you&lt;br/&gt; &lt;pre class="studentCodeColor" id="preBehindMyTextarea"&gt; &lt;/pre&gt; &lt;textarea id="myTextarea" class="studentCode" cols="100" rows="30" onkeyup="document.selection?ieTaKeyUp():taKeyUp();"&gt; &lt;/textarea&gt; &lt;div style="width:100px;height:60px;position:absolute;border:1px solid red;background-color:yellow" id="autoCompleteSelector"&gt; autocomplete contents &lt;/div&gt; &lt;script&gt; var myTextarea = document.getElementById('myTextarea'); var preBehindMyTextarea = document.getElementById('preBehindMyTextarea'); var autoCompleteSelector = document.getElementById('autoCompleteSelector'); function ieTaKeyUp(){ var r = document.selection.createRange(); autoCompleteSelector.style.top = r.offsetTop; autoCompleteSelector.style.left = r.offsetLeft; } function taKeyUp(){ taSelectionStart = myTextarea.selectionStart; preBehindMyTextarea.innerHTML = myTextarea.value.substr(0,taSelectionStart)+'&lt;span id="cursorPos"&gt;'; cp = document.getElementById('cursorPos'); leftTop = findPos(cp); autoCompleteSelector.style.top = leftTop[1]; autoCompleteSelector.style.left = leftTop[0]; } function findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { do { curleft += obj.offsetLeft; curtop += obj.offsetTop; } while (obj = obj.offsetParent); } return [curleft,curtop]; } //myTextarea.selectionStart &lt;/script&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload