tags:

views:

153

answers:

2

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?

+9  A: 

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');
cloudhead
Thanks cloudhead
mamu
+3  A: 

@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.

artlung
There is a syntax problem with your first example. You need to have a colon between key / value, not a comma.
TM
Ack! Thanks so much @TM. Fixed it.
artlung