Update: I’ve now created a jQuery font picker v2
I needed a way to quickly and easily generate a list of web safe fonts for the theme editor in a new CMS I am working on.
So I quickly rustled up the following plugin – it’s very basic, and I initially had it showing the fonts in the select dropdown by adding CSS to the <option> tags – however good old IE didn’t seem to support this.
I hope to rewrite this plugin someday to show instead a nice div overlay with the actual fonts to choose from, but until then…
/**
* Font selector plugin
* turns an ordinary input field into a list of web-safe fonts
* Usage: $('select').fontSelector(options);
* You can pass in the following options:
* value : the initial value of the select field
* onChange : a function to call when a new value is chosen
*
* Author : James Carmichael
* Website : www.siteclick.co.uk
* License : MIT
*/
jQuery.fn.fontSelector = function(settings) {
var fonts = new Array(
'Arial,Arial,Helvetica,sans-serif',
'Arial Black,Arial Black,Gadget,sans-serif',
'Comic Sans MS,Comic Sans MS,cursive',
'Courier New,Courier New,Courier,monospace',
'Georgia,Georgia,serif',
'Impact,Charcoal,sans-serif',
'Lucida Console,Monaco,monospace',
'Lucida Sans Unicode,Lucida Grande,sans-serif',
'Palatino Linotype,Book Antiqua,Palatino,serif',
'Tahoma,Geneva,sans-serif',
'Times New Roman,Times,serif',
'Trebuchet MS,Helvetica,sans-serif',
'Verdana,Geneva,sans-serif' );
return this.each(function(){
// Store the input field
var sel = this;
/* add individual font divs to fonts */
jQuery.each(fonts, function(i, item) {
// Add option to selector
jQuery(sel).append('<option value="' + item + '">' + item.split(',')[0] + '</option>');
// Select initial value
if(item == settings.value)
jQuery(sel).children('option:last').attr('selected', 'selected');
})
// Bind onchange event handler
if(settings.onChange)
jQuery(sel).bind('change', settings.onChange);
});
}
“Pick a comment style… any comment style…”
nice little plugin… thank you for sharing it. and if anyone is interested, changing line 39 to:
jQuery(sel).append(” + item.split(‘,’)[0] + ”);
will style each font name in the drop down as it’s font style… which should work correctly in most modern browsers…
well that didn’t work… ugh… James, you know what I meant:
option style=”font-family:’ + item.split(‘,’)[0] + ‘”
-grrrr
Hi Nick,
Thanks for your comment! I’ll see if I can modify the code at some point.
I think I did try something similar, and found it didn’t work in Internet Explorer.
A guy called Mike kindly posted this the other day where this blog used to be located – thanks Mike!
came across your plugin a few days ago. For my purposes I needed a somewhat more slick version, so I basically did what you said you hoped to do “someday” – wrote my own version (http://www.regios.org/jquery-font-picker-plugin) which can take an arbitrarily styled div and display an overlayed div with font preview. Used your font list, hope you don’t mind
Any chance of an example page with this? Sorry I’m about a year too late
Trying to implement it on a page but it is only putting in the first option.. i.e Arial. Not sure what is going wrong.
Hi Rob,
I have released an updated version complete with demo:
http://www.fullfatcode.com/2011/04/10/jquery-font-selector-version-2/
Hope you find it useful