User changing font in text area

Posted on 2003-12-09
Last Modified: 2013-12-03
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?

Question by:wjdashwood
  • 3
  • 2
  • 2
  • +1
LVL 10

Expert Comment

ID: 9904561
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.

LVL 10

Assisted Solution

Nushi earned 150 total points
ID: 9904601
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


Author Comment

ID: 9905651
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!


Accepted Solution

gam3r_3xtr3m3 earned 200 total points
ID: 9908481
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>
LVL 25

Assisted Solution

devic earned 150 total points
ID: 9909144
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.

Assisted Solution

gam3r_3xtr3m3 earned 200 total points
ID: 9911326
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>

Author Comment

ID: 9911753
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,

Author Comment

ID: 9964375
I've figured it out now, but as you have helped me reach the solution I have shared the points equally.


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with Javascript to round decimal points to 2 13 32
Non-Resizable Pharagraph 2 18
Worldmap 1 28
ExcelWebAddIn Connect to SQL database (Javascript or 7 17
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question