Raziel
Raziel

Reputation: 1566

Making text bold in jQuery

I'm currently having a problem replacing the current text with the same text but in bold. I've tried the following

var valueInRow = $(value).closest(".label.imaged").text();
var result = valueInRow.bold();
$(value).closest(".label.imaged").text().replaceWith(result);

But I'm not sure why it doesn't work; Any ideas?

Upvotes: 0

Views: 17796

Answers (3)

Sudharsan S
Sudharsan S

Reputation: 15393

Use .css() method also

var valueInRow= $(value).closest(".label.imaged");
valueInRow.css('font-weight', 'bold');

Upvotes: 3

DBS
DBS

Reputation: 9959

The existing answers are entirely valid, but I'll add this an alternative that doesn't add any new HTML elements or inline styles.

Creating a CSS rule that's purpose is simply to bold text, such as:

.boldText{ font-weight: bold !important; }

Will then allow you to bold any element simply by adding that class:

$(value).closest(".label.imaged").addClass("boldText");

*Note about the use of !important: This is usually not recommended CSS as it's often used in the wrong way. However in this case if you add a class called boldText to an element, chances are, you will always want it to have bold text.

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

You can clearly use .wrapInner():

Wrap an HTML structure around the content of each element in the set of matched elements.

$(value).closest(".label.imaged").wrapInner("<strong />");

Upvotes: 3

Related Questions