Tutorial :jQuery.attr function



Question:

i am using

$('<div/>').attr('font-weight','bold');  

It is not applying font-weight to bold with above function. Is there something wrong in there?


Solution:1

Yea, something's wrong, the $() function is used to select an element, class or id, not a mal-formed tag. Also, you should use the css() function to change css properties so you need something like this:

$("#mydiv").css('font-weight', 'bold');  

If you want to select all divs, you can do this:

$("div").css('font-weight', 'bold');  


Solution:2

@cloudhead has answered this well, but if you want to alter multiple css properties at one time it would look like:

$("div").css({'font-weight': 'bold', 'font-size': '3em'});  

Alternately, you could use jQuery's chaining method. Not as efficient, but it's a nice taste of the kind of flexibility jQuery has.

$("div").css('font-weight', 'bold').css('font-size', '3em');  

I encourage you to look at the jQuery Tutorials to get a better sense of what jQuery can do.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »