Populate rich text field using javascript

I’m using java script code to populate the form with some text when clicking in a div. It work fine for text fields or text area fields, but not on rich text fields. Can any one help me on this?

Here is the code I’m using:

function copiarTexto(numeroDiv) {
    var divAlvo = document.getElementById('textoModelo-' + numeroDiv);
  var divAlvo2 = document.getElementById('textoModelo2-' + numeroDiv);
  var divAlvo3 = '';
  var divAlvo4 = document.getElementById('textoModelo4-' + numeroDiv);
    var divAlvo5 = document.getElementById('textoModelo5-' + numeroDiv);
    var campoFormulario = document.getElementsByName('form-field-item_title')[0]; // Usa sempre o mesmo campo, se houver mais campos com o mesmo nome o número indica a posição
   var campoFormulario2 = document.getElementsByName('form-field-ref')[0]; 
  var campoFormulario3 = document.getElementsByName('form-field-quantity')[0]; 
   var campoFormulario4 = document.getElementsByName('form-field-value')[0]; 
   var campoFormulario5 = document.getElementsByName('form-field-description')[0]; 

    if (divAlvo && campoFormulario) {
        campoFormulario.value = divAlvo.innerText;
      campoFormulario2.value = divAlvo2.innerText;
      campoFormulario3.value = divAlvo3.innerText;
      campoFormulario4.value = divAlvo4.innerText;
      campoFormulario5.value = divAlvo5.innerText;
    } else {
        console.error('Div ou campo não encontrado');

I upload an image to show it.

I found the solution myself. I use this code to pass the text to the paragraph inside the div on the rich text field with the id brxe-rwpzhl : you will have to adapt to your case.

var targetDiv = document.getElementById('brxe-rwpzhl');
    var nestedParagraph = targetDiv.querySelector('#brxe-rwpzhl > div > div > p'); // Adjust the selector based on your structure

and this

 nestedParagraph.textContent = divAlvo5.innerText;
1 Like