User changing font in text area

Hello,
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:

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

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;
               newRng.select();
          }
       parent.document.execCommand(parent.command, false, chosenfont);
       parent.document.focus();
       parent.document.styles.src='images/buttons/font.gif';
}

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?

Thanks,
H
wjdashwoodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NushiCommented:
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.

Nushi.
0
NushiCommented:
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>

</textarea>

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

Nushi.
0
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() {
      selectStyle(this.id);
      parent.document.getElementById("style").style.visibility="hidden";
}
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gam3r_3xtr3m3Commented:
here's a snipplet:

<script>
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";}}
</script>
<body onLoad="chgText();">
<form name="frm1">
<select onChange="chgText();" name="select1">
<option>Verdana</option>
<option>Trebuchet MS</option>
<option>Monotype Corsiva</option>
<option>Arial</option>
<option>Helvetica</option>
<option>Sans-Serif</option>
</select>
</form>
<br />
<div id="text1">Hello! This is a changing font</div>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devicCommented:
here is an example
============================
<script>
var seltext="";
function changefont(fontz)
{
      var newtext="<span style=font-family:"+fontz+">"+seltext+"</span>"
      document.getElementById("edit").innerHTML=document.getElementById("edit").innerHTML.replace(seltext,newtext);
      
}
function changecolor(colorz)
{
      var newtext="<span style=color:"+colorz+">"+seltext+"</span>"
      document.getElementById("edit").innerHTML=document.getElementById("edit").innerHTML.replace(seltext,newtext);
      
}
function removeall()
{
      document.getElementById("edit").innerHTML=document.getElementById("edit").innerText;
}
</script>
<body>
<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>
<br><br>

<div id="edit" onkeyup="seltext=document.selection.createRange().text"
onmouseup="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.
</div>
0
gam3r_3xtr3m3Commented:
here's something i did before. maybe you'd need it:

<script>
function initAll(){chgText(document.frm1.select1.value);
chgColor(document.frm1.select2.value);
chgSize(document.frm1.select3.value,document.frm1.select4.value);
chgAlign(document.frm1.select5.value);
chgBold(document.frm1.bold.checked);
chgItalic(document.frm1.italic.checked);}
function chgText(font){if(font=="pre"){document.getElementById("pretext").style.visibility="visible";}
else{document.getElementById("pretext").style.visibility="hidden";document.getElementById("edit").style.fontFamily=font;}}
function chgTextPre(font){document.getElementById("edit").style.fontFamily=font;}
function chgColor(color){if(color=="pre"){document.getElementById("pre").style.visibility="visible";}
else{document.getElementById("pre").style.visibility="hidden";document.getElementById("edit").style.color="#"+color;}}
function chgColorHex(color1,color2,color3){re=/^([A-E]|[0-9])([A-E]|[0-9])$/;
if((re.test(color1.toUpperCase())==true)&&(re.test(color2.toUpperCase())==true)&&(re.test(color3.toUpperCase())==true)){
document.getElementById("edit").style.color="#"+color1.toUpperCase()+color2.toUpperCase()+color3.toUpperCase();}
else{alert("Error on hexadecimal character input!!!");}}
function chgSize(size,unit){if(size=="pre"){document.getElementById("presize").style.visibility="visible";}
else{document.getElementById("presize").style.visibility="hidden";document.getElementById("edit").style.fontSize=size+unit;}}
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";}
else{document.getElementById("edit").style.fontWeight="normal";}}
function chgItalic(italic){if(italic==true){document.getElementById("edit").style.fontStyle="italic";}
else{document.getElementById("edit").style.fontStyle="normal";}}
</script>
<body onLoad="initAll();">
<form name="frm1">
<table><tr><td>
Font:</td>
<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>
</select></td>
<td><span id="pretext"> <input type="text" name="font1" size="10" />
 <input type="button" value="Change Font" onClick="chgTextPre(document.frm1.font1.value);" />
</span></td></tr>
<tr><td>
Color:</td>
<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>
</select></td>
<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);" />
</span></td></tr>
<tr><td>
Size:</td>
<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>
<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>
</select></td>
<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);" />
</span></td></tr>
<tr><td>
Align:</td>
<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>
</select></td></tr>
<tr><td>Style:</td>
<td><table>
<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>
</table>
</td></tr>
</table>
</form>Format sample:
<div id="edit">Hello! This is a text which has changing fonts and colors! Try me!</div>
</body>
0
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;
               newRng.select();
          }
       parent.document.execCommand(parent.command, false, color);
       parent.document.focus();    
       parent.document.forecolor.src='images/buttons/textcolour.gif';
}

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,
H
0
wjdashwoodAuthor Commented:
I've figured it out now, but as you have helped me reach the solution I have shared the points equally.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.