How do I make an execCommand change the font type for my wysiwyg?

user1927513 picture user1927513 · Jan 7, 2013 · Viewed 7.5k times · Source

I can make the font bold using a button but not when choosing fonts using the select tag. I have a series of choices such as Arial, Times, Courier etc. with no way of clicking. This is what the code looks like

function fontEditor(){
var x=document.getElementById("fontName").selectedIndex;
var y=document.getElementById("fontName").options;
document.execCommand(x,"",y);
edit.document.focus(fontName);
}

Along with this

<select id="fontName" onChange="fontEditor('font',[selectedIndex].value)">
<option value="Arial">Arial</option>
<option value="Calibri">Calibri</option>
<option value="Comic Sans MS">Comic Sans MS</option>
</select>

Answer

K-Gun picture K-Gun · Jan 7, 2013

Try changing function fontEditor() to function fontEditor(fontName).

function fontEditor(fontName) {
    document.execCommand("fontName", false, fontName);
    ...
}

And:

<select onchange="fontEditor(this[this.selectedIndex].value)">
    <option value="Arial">Arial</option>
    <option value="Calibri">Calibri</option>
    <option value="Comic Sans MS">Comic Sans MS</option>
</select>