Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3879
  • Last Modified:

User changing font in text area

I'm trying to change the font in a  text area when the user selects a different font from a list. I have already done this for the colour of the text. I used the same code for the colour text but changed the options and the action performed. Everything works how it should but the font does not change.

The font is selected from a cell in a table an example cell is:

<td id="Verdana, Arial, Helvetica, sans-serif"><font face="Verdana, Arial, Helvetica, sans-serif" size="-1">Verdana</font></td>

and the code to change the font is:

function selectStyle(chosenfont)
       var sel = document.selection;
          if (sel!=null) {
               var newRng = sel.createRange();
               newRng = parent.rng;
       parent.document.execCommand(parent.command, false, chosenfont);

The id is sent correctly as this part is the same as for the text colour.
Any ideas as to why this does not work?

  • 3
  • 2
  • 2
  • +1
4 Solutions
can you please paste the rest of your code?
i cant see how did you get to the <font...>
and how do you try to cahnge the color.

as far as i can tell without looking at the rest of
your code (i can see that you have some table)
a textarea cannot contains formatted HTML inside

this will not work in textarea

<textarea rows="10" cols="50">
This is my text
<font color='red'> It is in the text area</font>


its not clear to me where does the formatted text goes to

wjdashwoodAuthor Commented:
it is not really a textarea, but a div tag with the id "edit"
( <div id="edit" onkeyup="reset(); markSelectedElement();" contenteditable="true" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%" onclick="reset(); markSelectedElement()"  onmouseup="markSelectedElement();" height="100%" width="100%">
Type your text here...</div> )

the code below gets the id of the cell, which is the font and passes it to the other section. I'm sorry i can't put all of the code here as its huge and in many different files. If there are any other parts you need then i can paste them.

function click() {
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

here's a snipplet:

function chgText(){document.getElementById("text1").style.fontFamily=getFont(document.frm1.select1.selectedIndex);}
function getFont(selected1){
if(selected1=="0"){return "Verdana";}
if(selected1=="1"){return "Trebuchet MS";}
if(selected1=="2"){return "Monotype Corsiva";}
if(selected1=="3"){return "Arial";}
if(selected1=="4"){return "Helvetica";}
if(selected1=="5"){return "Sans-Serif";}}
<body onLoad="chgText();">
<form name="frm1">
<select onChange="chgText();" name="select1">
<option>Trebuchet MS</option>
<option>Monotype Corsiva</option>
<br />
<div id="text1">Hello! This is a changing font</div>
here is an example
var seltext="";
function changefont(fontz)
      var newtext="<span style=font-family:"+fontz+">"+seltext+"</span>"
function changecolor(colorz)
      var newtext="<span style=color:"+colorz+">"+seltext+"</span>"
function removeall()
<button onclick=changefont(this.innerHTML)>Arial</button>
<button onclick=changefont(this.innerHTML)>Helvetica</button>
<button onclick=changefont(this.innerHTML)>Sans-Serif</button><br><br>

<button onclick=changecolor(this.innerHTML)>red</button>
<button onclick=changecolor(this.innerHTML)>blue</button>
<button onclick=changecolor(this.innerHTML)>green</button><br><br>

<button onclick=removeall()>remove all</button><br><br>

<div id="edit" onkeyup="seltext=document.selection.createRange().text"
contenteditable="true" style="OVERFLOW: auto;
WIDTH: 100%; HEIGHT: 100%" height="100%" width="100%">
Type your text here:
I'm trying to change the font in a  text
area when the user selects a different font
from a list. I have already done this for the
colour of the text. I used the same code for the
colour text but changed the options and the
action performed. Everything works
how it should but the font does not change.
here's something i did before. maybe you'd need it:

function initAll(){chgText(document.frm1.select1.value);
function chgText(font){if(font=="pre"){document.getElementById("pretext").style.visibility="visible";}
function chgTextPre(font){document.getElementById("edit").style.fontFamily=font;}
function chgColor(color){if(color=="pre"){document.getElementById("pre").style.visibility="visible";}
function chgColorHex(color1,color2,color3){re=/^([A-E]|[0-9])([A-E]|[0-9])$/;
else{alert("Error on hexadecimal character input!!!");}}
function chgSize(size,unit){if(size=="pre"){document.getElementById("presize").style.visibility="visible";}
function chgSizePre(size,unit){if(!isNaN(size)){document.getElementById("edit").style.fontSize=size+unit;}
else{alert("Error on size input!!!");}}
function chgAlign(align){document.getElementById("edit").style.textAlign=align;}
function chgBold(bold){if(bold==true){document.getElementById("edit").style.fontWeight="bold";}
function chgItalic(italic){if(italic==true){document.getElementById("edit").style.fontStyle="italic";}
<body onLoad="initAll();">
<form name="frm1">
<td><select onChange="chgText(this.value);" name="select1">
<option value="Times New Roman">Times New Roman</option>
<option value="Verdana">Verdana</option>
<option value="Trebuchet MS">Trebuchet MS</option>
<option value="Monotype Corsiva">Monotype Corsiva</option>
<option value="Arial">Arial</option>
<option value="Helvetica">Helvetica</option>
<option value="Sans-Serif">Sans-Serif</option>
<option value="pre">Predefine...</option>
<td><span id="pretext"> <input type="text" name="font1" size="10" />
 <input type="button" value="Change Font" onClick="chgTextPre(document.frm1.font1.value);" />
<td><select onChange="chgColor(this.value);" name="select2">
<option value="000000">Black</option>
<option value="FF0000">Red</option>
<option value="0000FF">Blue</option>
<option value="00FF00">Green</option>
<option value="FFFF00">Yellow</option>
<option value="FF00FF">Magenta</option>
<option value="00FFFF">Cyan</option>
<option value="pre">Predefine...</option>
<td><span id="pre">#
 <input type="text" name="clr1" maxlength="2" size="1" />
 <input type="text" name="clr2" maxlength="2" size="1" />
 <input type="text" name="clr3" maxlength="2" size="1" />
 <input type="button" value="Change Color" onClick="chgColorHex(document.frm1.clr1.value,document.frm1.clr2.value,document.frm1.clr3.value);" />
<td><select onChange="chgSize(this.value,document.frm1.select4.value);" name="select3">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="10">10</option>
<option value="12">12</option>
<option value="14">14</option>
<option value="18">18</option>
<option value="24">24</option>
<option value="36">36</option>
<option value="72">72</option>
<option value="pre">##</option>
<select onChange="chgSize(document.frm1.select3.value,this.value);" name="select4">
<option value="pt">pt</option>
<option value="px">px</option>
<option value="em">em</option>
<td><span id="presize"><input type="text" name="size1" maxlength="3" size="2" />
 <input type="button" value="Change Size" onClick="chgSizePre(document.frm1.size1.value,document.frm1.select4.value);" />
<td><select onChange="chgAlign(this.value);" name="select5">
<option value="left">Left</option>
<option value="center">Center</option>
<option value="right">Right</option>
<option value="justify">Justify</option>
<tr><td><input type="checkbox" value="bold" name="bold" onClick="chgBold(this.checked);"></td><td>Bold</td></tr>
<tr><td><input type="checkbox" value="italic" name="italic" onClick="chgItalic(this.checked);"></td><td>Italic</td></tr>
</form>Format sample:
<div id="edit">Hello! This is a text which has changing fonts and colors! Try me!</div>
wjdashwoodAuthor Commented:
devic's exapmle is the closest to what I want, but I don't really what some code to replace the code I already have. I have a wsywig editor that already changes the text colour, makes the text bold, italic etc. What I need to do is get the code working for the font changing. I have a <div> which loads a file with a table, each cell of the table is a different font, each id is the font name or font family. What I need is how to use that id to make the selected text that font.

The colour works by passing the id (the colour in hex) to the following function:
function selectColour(color)
       var sel = document.selection;
          if (sel!=null) {
               var newRng = sel.createRange();
               newRng = parent.rng;
       parent.document.execCommand(parent.command, false, color);

I wanted to do the same thing with the font, use the id and pass it into a function similar to this. When I attempted this the font will not change. I thought it was because I was passing it in the wrong format.
Can anyone explain why this method won't work for the font changing, or if it will help me make it work.

Thanks, all your help is appreciated,
wjdashwoodAuthor Commented:
I've figured it out now, but as you have helped me reach the solution I have shared the points equally.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now