I need to change the source of a text-Area: Ex: Bold to italic, but I haven’t been able to yet. Can someone help me?

Asked

Viewed 96 times

-1

var textarea = document.querySelector(".editor");
var negrito = document.querySelector("button");
var paragrafo = document.createElement('p');


negrito.onclick = function(){ 
    var select =  document.getSelection(".editor");
    var italico = document.createElement('i');
    var texto = document.createTextNode(select.toString());
    italico.appendChild(texto);
    console.log(select.italico());

}

textarea.onkeypress= function(){



}

1 answer

0

In the example below I take the value selected by the user, use a regex to get only the text of the textarea, and create a new text by adding the tag selected by the user and finally add this new text to the textarea.

function opcaoSelecionada(val){
  if(val) {
    let text = document.getElementById('area').value.replace(/(<(&nbsp;|[^>]+)>)/ig, "");
    text = `<${val}>${text}</${val}>`;
    document.getElementById('area').value = text;
  }

}
<textarea id="area"></textarea>

<select onchange="opcaoSelecionada(this.value)">
      <option value="">Selecione uma opção</option>
      <option value="b">Negrito</option>
      <option value="i">Italico</option>
</select>

Browser other questions tagged

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