I need to insert some macros into a text area, these macros can be inserted anywhere in the field, currently I only use a append
that just puts the macro in the beginning.
I'm thinking of getting the coordinates of the click with clientX - ClientY
and when inserting the macro to use these coordinates for insertion.
Example:
$("#inserirMacro").click(function() {
// Pega o macro selecionado.
var macro = $("#macros :selected").text();
// Inseri o macro no começo do campo textarea
$('#campo').append(macro);
});
// Pega as coordenadas do click no textArea.
$("#campo").mousedown(function(e) {
console.log(e.clientX + ' ClientX ' + e.clientY + ' ClientY');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><selectid="macros">
<option>[NOMECLIENTE]</option>
<option>[CPF]</option>
<option>[TELEFONE]</option>
<option>[TELEFONE2]</option>
</select>
<button type="button" id="inserirMacro">Inserir</button>
<br>
<textarea rows="4" id="campo"></textarea>
How can I use these coordinates to insert the macros in the correct places?