Note that there are some explanatory texts on larger screens.

plurals
  1. POFind carret position x y inside textarea with javascript
    text
    copied!<p>What Im trying to do is - i dont know the name maybe - a Prediction Help Inputter inside a textarea. It uses jquery autocomplete. When the user types '[[g ' inside textarea (id=test), a input with autocomplete is opened (id=example), so it search in 'data'. When the user find the desired data, he must press Shif+Enter to insert the data into the textarea, closing with ']]'.</p> <p><strong>How could I find the position of the carret to make the input appears near there?</strong></p> <p>I dont want to find the index of the carret, but something like the x y absolute position.</p> <p>What do you suggest me?</p> <p>Code above:</p> <pre><code>&lt;textarea onkeydown="predicao(this);" cols="40" rows="10" id="test" onfocus="this.focus()"&gt;&lt;/textarea&gt; &lt;input id="example" style="display: none;" onkeyup="insert(this, event);"/&gt; &lt;script language="Javascript"&gt; &lt;!-- function predicao(objeto){ comprimento = objeto.value.length; var antipenultimo = comprimento - 4; var input = objeto.value.substring(antipenultimo,comprimento); var output = ""; for(i=0; i&lt;input.length; ++i){ if(output != "") output += ", "; output += input.charCodeAt(i); } if (output == "91, 91, 103, 32"){ var preditor = document.getElementById('example'); preditor.value = ''; preditor.style.display = 'block'; preditor.focus(); preditor.select(); } } function insert(objeto, evt){ var e = evt || event; var code = e.keyCode || e.which; if(e.shiftKey &amp;&amp; code == '13') { var texto = document.getElementById('test').value; texto += objeto.value+']]'; document.getElementById('test').focus(); document.getElementById('test').value = texto; objeto.style.display = 'none'; } } $(document).ready(function(){ var data = "Afrikaans Català Deutsch English Esperanto Suomi Français Galego Hrvatski Magyar Bahasa Indonesia Italiano Basa Jawa".split(" "); $("#example").autocomplete(data);}); &lt;/script&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