Add content at cursor position with Froala editor

Asked

Viewed 94 times

0

I have a textarea field and a second field select.
I click on a certain place of mine textarea, and then choose a
options in my select field, and I want that option that is chosen(any text), appear in the place I last clicked on in my textarea.
Obs. My application is in Ruby on Rails and in my textarea, I use the Froala Editor

  • can post the code, will be better to help you

1 answer

0

Place an onclick in the textarea to call a function to pick up the cursor position and save to a global variable.

Use the code:

 document.getElementById("IdTextarea").selectionStart;

Then create a function that takes the selected select value with onchange and Selected index (or the way you prefer) and enter the value at the previously collected position.

conteudo = document.getElementById("IdTextarea").value;             
pt1 = conteudo.substring(0,pos); 
pt2 = conteudo.substring(pos);
document.getElementById("IdTextarea").value = pt1 + selecionado + pt2;

where the pos variable is equal to the value of the cursor position you collected in the other function.

Browser other questions tagged

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