Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a text in font/weight style bold in JavaScript

Tags:

javascript

I need change the font weight for a text element in JavaScript:

My code here does not work:

var btn = document.getElementById('accessibilityButton');
btn.innerHTML = 'Default Text';
btn.innerHTML.style.fontWeight = 'bold';

What am I doing wrong?

Please note that I don't want to use any libraries (jQuery et. al.) and am looking for a plain JS solution.

like image 579
GibboK Avatar asked Oct 01 '12 12:10

GibboK


People also ask

How do I make text bold in JavaScript?

To create a bold text using JavaScript, use the bold() text. This method causes a string to be displayed as bold as if it were in a <b> tag.

How do you make the text bold font weight bold?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”

How do you change the font of text in JavaScript?

To change or set a font style for certain text, the fontFamily CSS property needs to be changed. The fontFamily property sets or returns a list of font-family names for text in an element. To change the font style by option dropdown: The font values can be passed in option tags using option value.

What is bold font weight CSS?

400 – Normal. 500 – Medium. 600 – Semi Bold (Demi Bold) 700 – Bold.


2 Answers

You need to use:

btn.style.fontWeight = 'bold';

as it's a property of the element itself.

See: http://jsfiddle.net/6ypS8/

like image 119
m90 Avatar answered Nov 07 '22 04:11

m90


You should apply your style directly on your button not on button.innerHTML :

btn.style.fontWeight = 'bold';
like image 21
Pierre Avatar answered Nov 07 '22 04:11

Pierre