Add content at cursor position with Froala editor

0

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

    
asked by anonymous 11.12.2017 / 22:45

1 answer

0

Place an onclick on the textarea to call a function to grab the cursor position and save it to a global variable.

Use the code:

 document.getElementById("IdTextarea").selectionStart;

Then create a function that takes the value of the selected select with onchange and selected index (or whatever you prefer) and enter the value in 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 variable pos is equal to the value of the cursor position you collected in the other function.

    
12.12.2017 / 13:51