Solved

User changing font in text area

Posted on 2003-12-09
8
3,871 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:wjdashwood
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 10

Expert Comment

by:Nushi
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.

Nushi.
0
 
LVL 10

Assisted Solution

by:Nushi
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>

</textarea>

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

Nushi.
0
 

Author Comment

by:wjdashwood
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() {
      selectStyle(this.id);
      parent.document.getElementById("style").style.visibility="hidden";
}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 7

Accepted Solution

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

Assisted Solution

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

Assisted Solution

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

Author Comment

by:wjdashwood
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;
               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
 

Author Comment

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

Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now