1
I have a textarea
where I need to add predefined texts that are my links. When clicking on them, add the content of ID
the current position of the cursor on textarea
. I need to do this with jQuery.
Example: https://jsfiddle.net/e7kva60d/
1
I have a textarea
where I need to add predefined texts that are my links. When clicking on them, add the content of ID
the current position of the cursor on textarea
. I need to do this with jQuery.
Example: https://jsfiddle.net/e7kva60d/
1
Set the content on id
field would not be the best choice. Recalling that the id
is to store the identifier of your field to manipulate it. to solve your problem and get more semantic, it would be advisable to use a date field to store the value as below:
HTML:
<textarea name="descricao" class="form" rows="9"></textarea>
<a href="#" data-message="Bom dia, como voce esta?">Saudação</a>
<a href="#" data-message="texto do exemplo numero 2">Exemplo 2</a>
<a href="#" data-message="texto do exemplo numero 3">Exemplo 3</a>
<a href="#" data-message="texto do exemplo numero 4">Exemplo 4</a>
Javascript:
function setarValor() {
var menssagem = $(this).data('message'); //Pego a menssagem do campo data
var textAreaPosition = $('textarea').prop("selectionStart"); //pego a posição do cursor no textarea
var textAreaValue = $('textarea').val(); // pego valor do text area
var valorConcatenado = [textAreaValue.slice(0, textAreaPosition), menssagem, textAreaValue.slice(textAreaPosition)].join(''); // faço a concatenação da menssagem na posição
$('textarea').val(valorConcatenado); //seto valor no textarea
}
//Ele execulta após toda a pagina ser renderizada
$(document).ready(function () {
//seto de click nos seus links
$('a').click(setarValor);
});
Follows jsfiddle of amendment
Browser other questions tagged javascript jquery
You are not signed in. Login or sign up in order to post.
Good evening Bruno, Thanks for your tip.
– Diego Marquez
would thus https://jsfiddle.net/buh159/e7kva60d/1/
– Brunno
The script is working, but it deletes the previous text when you click on more than one item, you can make it always add and not delete the previous text?
– Diego Marquez
has yes.. look at this link https://jsfiddle.net/buh159/e7kva60d/3/ if this is the case I will update the post above
– Brunno
Dude, this 99%, I’ll put more difficulty :P You have to put where the mouse is positioned, or between two sentences for example "hello like this [I WANT TO PUT HERE] All right yes etc etc etc etc"
– Diego Marquez
I didn’t understand what you meant by where the mouse is positioned.. could explain :D It would be the content of the link ?
– Brunno
If you notice it adds msg in the last line, then if I want to add a predetermined message where there is already a text I can’t... try to write any text and try to add an msg in the first line.. it’s not going to be possible because he always adds on the last line... I could understand?
– Diego Marquez
The ideal is that he added where I was the mouse click inside the textarea...
– Diego Marquez
I think I understand, I’m going to implement ..
– Brunno
follow the implementation :D https://jsfiddle.net/buh159/e7kva60d/6/ other improvements are possible, like to trigger space before and after the message, but this goes from you
– Brunno
For now this perfect guy, very good indeed. Passes me an email for me to buy lunch for you ;P
– Diego Marquez
hahaha' I’m glad it worked out. If you need us, we’re there.. Don’t forget to close the question :D
– Brunno