I'm not able to change font size using Jquery. I want to change font size of a div. I have defined default font size for body as 12. I tried to change it as follows, but it didn't work :(
$("#SelFontSize").change(function(){ $("#"+styleTarget).css({'font-size':'"+$(this).val()+"px'}); });
To change the font size of an element, we will use css() method. The css() method is used to change the style property of the selected element. Return value: It will return the value of the property for the selected element.
In HTML, you can change the size of text with the <font> tag using the size attribute. The size attribute specifies how large a font will be displayed in either relative or absolute terms. Close the <font> tag with </font> to return to a normal text size.
To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size. HTML5 do not support the <font> tag, so the CSS style is used to add font size.
The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.
Try:
$("#"+styleTarget).css({ 'font-size': $(this).val() });
By putting the value in quotes, it becomes a string, and "+$(this).val()+"px
is definitely not close to a font value. There are a couple of ways of setting the style properties of an element:
Using a map:
$("#elem").css({ fontSize: 20 });
Using key and value parameters:
All of these are valid.
$("#elem").css("fontSize", 20); $("#elem").css("fontSize", "20px"); $("#elem").css("font-size", "20"); $("#elem").css("font-size", "20px");
You can replace "fontSize"
with "font-size"
but it will have to be quoted then.
Not saying this is better, just another way:
$("#elem")[0].style.fontSize="20px";
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