Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if font weight is normal or bold

I'm setting the font-weight property with the following code:

$(this).css({ 'font-weight': 'normal' });

Now i want to check if an element has bold or normal font-weight propety, how do i do this?

like image 599
Lord Vermillion Avatar asked Mar 11 '14 10:03

Lord Vermillion


People also ask

How do you know if a text is bold?

Highlight the text and click on the link. document. queryCommandState("bold"); //That's the command I've been searching.

Is font weight inherited?

The CSS spec specifies that font-weight and font-size are inherited. This simply means that any element with no alternative rule for those properties will cause them to inherit the final, computed values from their parent.

What's the default font weight?

normal: This is the default font-weight & defines the normal font-weight. It is equal to the number value 400. bold: This defines the bold font-weight, it is equal to the number value 700.

Why font weight property is used?

The font-weight property can separately be used to set how thick or thin characters in text should be displayed.


2 Answers

you can get it using:

fontWeight = $(this).css('font-weight')

To compare:

if (fontWeight  == 'normal'|| fontWeight  == '400')
//or
if (fontWeight  == 'bold' || fontWeight  == '700')
like image 87
Milind Anantwar Avatar answered Oct 13 '22 00:10

Milind Anantwar


You can use:

if ($('#yourElement').css('font-weight') == 'normal') {
    // Your code if font-weight is normal
} else if($('#yourElement').css('font-weight') == 'bold') {
    // Your code if font-weight is bold
}
like image 33
Felix Avatar answered Oct 12 '22 23:10

Felix