JavaScript Characters: Quotation Marks

By: Dusty Arlia
Published on Wednesday, October 23, 2013, 08:06 AM
Last Updated on Saturday, July 11, 2015 at 7:49 PM
Total Updates: 3

When you style something with CSS, you would do it like this:

border: 1px solid red; font-size: 12px; font-family: Arial;

You will notice that there are no quotation marks. But if you would like to do the same styling with JavaScript, you will have to put the property values in quotation marks, because these are strings. Here is how you would apply the same CSS rules using JavaScript:

ex_element.style.border = "1px solid red";
ex_element.style.fontSize = "12px";
ex_element.style.fontFamily = "Arial";

If you are computing your property value, make sure you concatenate your unit of measurement in quotation marks. Concatenating your unit of measurement in quotation marks changes your numeric result into a string as a side effect. Here is an example:

//computed example
ex_element.style.width = (l_border + l_padding + body + r_padding + r_border) + " px";

//taking a numeric variable example
ex_element.style.fontSize = my_font_size + "px";

All the properties of the CSSStyleDeclaration object hold the values to your css properties. These values are strings and need to be placed in quotation marks:

ex_element_var.style.color = "red";
ex_element_var.style.fontSize = "12px";
ex_element_var.style.fontFamily='Arial';

Comments:

Ad:

Ad: