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

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

My code does not work here:

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

What am I doing wrong?

Please note that I do not want to use any libraries (jQuery et al.), And I am looking for a simple JS solution.

+5
source share
3 answers

You need to use:

btn.style.fontWeight = 'bold';

since this is a property of the element itself.

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

+16
source

You must apply your style directly on your button, not on the .innerHTML button:

btn.style.fontWeight = 'bold';
+5
source

Hold 'return false' to prevent postback.

<button id="btn" onclick="this.style.fontWeight = 'bold';return false;" >TEXT</button>
Run codeHide result
0
source

All Articles