Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set font size in jquery

Tags:

jquery

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'});     }); 
like image 885
KutePHP Avatar asked Jul 13 '10 11:07

KutePHP


People also ask

How to change font size in jQuery?

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.

How do you change font size in HTML?

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.

How do I increase and decrease font size in HTML?

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.

What is the use of this keyword in jQuery?

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.


2 Answers

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.

like image 129
Anurag Avatar answered Oct 09 '22 01:10

Anurag


Not saying this is better, just another way:

$("#elem")[0].style.fontSize="20px"; 
like image 30
husayt Avatar answered Oct 09 '22 02:10

husayt