How to change font in textarea using javascript?

4

Is it possible to change the text font of a textarea using javascript?

    
asked by anonymous 20.07.2017 / 21:23

4 answers

2

Using Jquery:

$('textarea').css('font-family', 'Arial');

Example:

$('div').on('click', function(){

  $('textarea').css('font-family', 'Arial');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>Depois de 10 anos o menino virou adulto.</textarea>
<div>Clique para mudar a fonte</div>
    
20.07.2017 / 21:25
2

document.getElementById('meuTextarea').style.fontFamily = 'nomequalquer';
document.getElementById("meuTextarea").style.fontSize = "xx-large";
document.getElementById("meuTextarea").style.color = "red";
document.getElementById("meuTextarea").style.wordSpacing = "10px"
@font-face {
  font-family: 'nomequalquer';
  src: url(https://fonts.gstatic.com/s/tangerine/v8/HGfsyCL5WASpHOFnouG-RFtXRa8TVwTICgirnJhmVJw.woff2);
}
<textarea id=meuTextarea>Olá George, gostou?</textarea>

You can download the font and publish it to the server.

Example with .TTF

CSS

@font-face {
  font-family: nomequalquer;
  //baixe a fonte e publique no seu servidor
  src: url('SANTO___.TTF');
}

JavaScript

  document.getElementById('meuTextarea').style.fontFamily = 'nomequalquer';
  document.getElementById("meuTextarea").style.fontSize = "xx-large";
  

Some other properties:

  • fontStyle - character style. Values: normal, italic, oblique, and inherit example document.getElementById("meuTextarea").style.fontStyle = "italic";

  • fontWeight - characters in bold or light weight. Values: bold, bolder, lighter, normal, 100, 200 .... example document.getElementById("meuTextarea").style.fontWeight = "bold";

  • textAlign - horizontal alignment. Values: center, justify, left and right. example document.getElementById("meuTextarea").style.textAlign = "center";

  • letterSpacing - spacing between characters. Values: usually in units. example document.getElementById("meuTextarea").style.letterSpacing = "1.2em";

  • wordSpacing - spacing between words. Values: usually in units. example document.getElementById("meuTextarea").style.wordSpacing = "20px";

21.07.2017 / 05:12
1

document.querySelector('textarea').style.fontFamily = 'Impact';
<textarea></textarea>
    
20.07.2017 / 21:26
1

Using only tagged according to your question and assuming that you use only one textarea:

document.getElementsByTagName('textarea')[0].style.fontFamily = 'Verdana';
<textarea>Teste</textarea>

If you have more than one textarea on your page, you can set an ID or a Name for your textarea, and then you can access and change it directly:

Let's say you've put a meuTextarea id in your textarea:

document.getElementById('meuTextarea').style.fontFamily = 'Verdana';
<textarea id=meuTextarea>Teste</textarea>
    
20.07.2017 / 21:28