nachshon f
nachshon f

Reputation: 3680

How to add bold text using javascript

I'm trying to add bold text to my website using javascript, but it literly shows the "<b>". How do i fix this?

var TextBox = document.getElementById("TextBox");

TextData.textContent = "<b> hello </b>"

I tried using the document.write(""); , but it changes the whole website. Any Suggestions?

Upvotes: 9

Views: 67067

Answers (3)

EricL
EricL

Reputation: 959

Toggle/add a css class on the containing element (TextBox).

document.getElementById('TextBox').className = 'bold';
.bold {
    font-weight: bold;
}
<div id='TextBox'>
    My text.
</div>

Or something like that.

Upvotes: 2

Kristijan Iliev
Kristijan Iliev

Reputation: 4987

You can add first the content to textbox like this:

TextBox.innerHTML = "Hello";

then add its style like this:

TextBox.style.fontWeight = 'bold';

Or you can do it setting the innerHTML property of the element, just like this:

 TextBox.innerHTML = "<b>Hello</b>";

I tend to set style separately. You should avoid adding style like this, better use css classes.

Upvotes: 19

Bastian Br&#228;u
Bastian Br&#228;u

Reputation: 801

TextBox.innerHTML = "<b> hello </b>"

Upvotes: 5

Related Questions