GibboK
GibboK

Reputation: 73908

How to make a text in font/weight style bold in 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.

Upvotes: 7

Views: 64931

Answers (3)

user4419908
user4419908

Reputation:

Keep 'return false' to prevent postback.

<button id="btn" onclick="this.style.fontWeight = 'bold';return false;" >TEXT</button>

Upvotes: 2

m90
m90

Reputation: 11812

You need to use:

btn.style.fontWeight = 'bold';

as it's a property of the element itself.

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

Upvotes: 19

Pierre
Pierre

Reputation: 1282

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

btn.style.fontWeight = 'bold';

Upvotes: 5

Related Questions