tags:

views:

326

answers:

3

I need some jQuery magic that will look for all text on a page with a given font (say Helvetica), and change the font of only that text to something else (say Arial).

+4  A: 

This should work, I checked it on my site and it changed font properly.

$('body').filter(function() {
    return ( $(this).css('font-family').indexOf('Helvetica')>-1);
}).css('font-family', 'Arial');
Thinker
This is the best solution of the two currently posted; but I still maintain that this isn't something you should do; there's probably a better way than using JS. I mean, search-and-replace in all your style sheets would certainly do the same thing.
You
@You - not that I disagree, but '...is the best solution' doesn't do much to explain why it is the best solution, it's a plain endorsement - it would be more useful to me, Thinker, and everyone else to put a bit of substance behind it.
karim79
@karim79: It's better because it uses `filter` instead of `each`; which makes for prettier and more readable/understandable code. Other than that the two solutions are exactly the same.
You
A: 

If everything were defined via CSS, have you ever thought of spawning your own @font-face definitions, overriding Helvetica?

For example, if everything is defined to render in Helvetica, like in this statement:

* {

    font-family: Helvetica;

}

You may then append a @font-face statement to your style sheet, which would now look like this:

@font-face {

    font-family: 'Helvetica';

    src: local('Anivers-Regular'),
     local('Anivers Regular'),
     url('typeface.Anivers.Regular.otf') format('truetype');

}

It works on in-line-styled elements like <h4 style="font-family: Helvetica;">aw</h4> too.

In that way, everything Helvetica would be automagically rendered in (, e.g.,) Anivers, and this snippet works with or without double quotes. List PostScript name, Full name + style, then an URI for cross-browser compatibility. If the fallback is on Helvetica, this CSS magic would work. Plus, you get free performance overhead in this way since the browser, instead of your script, does the substitution for you.

Unfortunately, Internet Explorer hates .otf files for no apparent reason and you would need an utility called ttf2eot just in case.

This is an ugly hack and shall live in your production code for too long.

By the way, what’s the scenario and why would you want to do this — substituting a better-looking typeface with an inferior one?

If you later changed your mind and would like to use the “real” Helvetica somewhere else, you will have to swizzle it under another name. Tedious work isn’t it.

Evadne Wu