Predetermined messages

Asked

Viewed 26 times

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 answer

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

  • Good evening Bruno, Thanks for your tip.

  • 1

    would thus https://jsfiddle.net/buh159/e7kva60d/1/

  • 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?

  • 1

    has yes.. look at this link https://jsfiddle.net/buh159/e7kva60d/3/ if this is the case I will update the post above

  • 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"

  • 1

    I didn’t understand what you meant by where the mouse is positioned.. could explain :D It would be the content of the link ?

  • 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?

  • The ideal is that he added where I was the mouse click inside the textarea...

  • 1

    I think I understand, I’m going to implement ..

  • 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

  • For now this perfect guy, very good indeed. Passes me an email for me to buy lunch for you ;P

  • 1

    hahaha' I’m glad it worked out. If you need us, we’re there.. Don’t forget to close the question :D

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.