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.
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.
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.”
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.
400 – Normal. 500 – Medium. 600 – Semi Bold (Demi Bold) 700 – Bold.
You need to use:
btn.style.fontWeight = 'bold';
as it's a property of the element itself.
See: http://jsfiddle.net/6ypS8/
You should apply your style directly on your button not on button.innerHTML :
btn.style.fontWeight = 'bold';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With