How to set a CSS font-family using JQuery when the font name has a space

abalter

I'm trying to set the font-family property using the JQuery css method and running into a problem when the font name has a space.

No matter how I quote or don't quote the font name, the element ends up like:

<h1 style="font-family: "Comic Sans"">Title Text</h1>

with nested double quotes.

var font_family = 'Arial Black';
var $title_template = $('<h1>Title Text</h1>');
var $title;

console.log(font_family)
$title = $title_template.clone().css({'font-family': font_family});
$('body').append($title)

font_family = "'Arial Black'";
console.log(font_family);
$title = $title_template.clone().css({'font-family': font_family});
$('body').append($title)

font_family = '"Arial Black"';
console.log(font_family);
$title = $title_template.clone().css({'font-family': font_family});
$('body').append($title)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 style="font-family: 'Arial Black'">Title Text</h1>

Benjamin Ray

If you are looking at the font in the developer options in your browser, don't be alarmed that it looks like poorly nested quotes: <h1 style="font-family: "Arial Black"">Title Text</h1>. That's just how the browser chooses to render the Style property of that element. The style is actually being applied, and the font family is being set properly.

If you look at the properties of the DOM element (in Chrome DevTools for example), you'll see that the value of font-family is "Arial Black". The extra quotes are there because of the space in the font name. And if you change the first line of your JS from var font_family = 'Arial Black to var font_family = 'Arial', the value of font-family will appear simply as Arial (without quotes).

PS - To get "Comic Sans" to appear I had to use "Comic Sans MS", the proper name of the infamous font.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set a font family to "erewhon" when using Latex in matplotlib?

How to set the font family I want in css?

Set matplotlib font by family name when there are multiple fonts with same name

Blank space bottom of text when using font family

how to set the UIFont family and name with display font on the UITableView?

How to set font family with javascript ONLY- no access to jquery, html, or css, only js

How to set the font-family like a digital clock CSS?

How to change the font family of Highchart to Bootstrap css default font family

How do you set a default font family when using a pre-made flutter theme?

How to set the font-family as specified when editing post in wordpress?

How do I change the font family of a paragraph when it is clicked? Im using jQuery

Set font-family to specific symbols in CSS

How to set column colors, title colors , font size , font family in external css file for highcharts?

Jquery to edit css font-family values

How to use font-family with same name?

How can i get list of font family(or Name of Font) in matplotlib

Change font-size according to font-family using CSS?

How to set font family but retain fallback stack?

How to set default font family in React Native?

TTF Font not being displayed when using @Font-Family

How font-family declaration works in CSS?

Html <font> tag has no effect when css values are set

Using CSS to set default font

selecting a font from the font-family in css

How do you use the font shorthand if your font has a space in it?

Set display font family in wxhaskell?

CSS Font family not working in Firefox

CSS: Format of font-family

Font-family CSS transition