Making font strong to normal using jquery not working

339
November 21, 2016, at 6:17 PM

I have a div like the following:

<div id="div1" ><strong>some text</strong></div>

I want to make the font normal using jquery. I tried as follows:

$("#div1").css("font-weight", "normal !important");

But it is not working!!

Answer 1

You need to select the <strong> not the div.

$("#div1 strong").css("font-weight", "normal");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div id="div1"><strong>some text</strong> 
</div>

Answer 2

What your code does is set the font weight for the content of the <div>, and using !important can help override other css settings to the same element.

There are 2 problems though:

One problem is that the content (text...) is inside a nested element, and that has it's own style. You can either set the style of the nested element, or remove it altogether.

The other is that !important doesn't work inside $().css(). So you should try to get rid of it if you can (to avoid complex solutions).

Assuming you need an element to wrap the text (for whatever reason), I'd suggest replacing it with a more appropriate descriptive element (or rather, less descriptive in this case) and play with the style as needed:

$("#myText").css("font-weight", "normal");
#myText { 
  font-weight: bold 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<div id="div1" ><span id="myText">some text</span></div>

Rent Charter Buses Company
READ ALSO
Laravel csrf token mismatch for ajax POST Request

Laravel csrf token mismatch for ajax POST Request

I am trying to delete data from database via ajax. .

648
each function jquery don&#39;t work

each function jquery don't work

hi I've this html code.

396
JQuery - Loop through each case of a class from a point

JQuery - Loop through each case of a class from a point

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. [nnn]My code currently is this:.

305
Get text from outer span without text of inner elements

Get text from outer span without text of inner elements

I want to extract some text from an HTML element in Selenium and Javascript. .

403