Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to change the css of an element using jQuery.

$('p').css({
    padding-left : '6px',
    border-left : '5px solid #555'
});

It does not work.

However, when i use

$('p').css('padding-left','6px');
$('p').css('border-left','5px'); 

it works...

What am I doing wrong here?

Thanks

share|improve this question
1  
Nothing, attribute names need to be quoted when using .css() – Diodeus Mar 28 at 14:00
By not having the quotes, it is interpreting padding-left as a variable, just wrap them in single quotes and you'll be all set. – mcpDESIGNS Mar 28 at 14:01

5 Answers

up vote 6 down vote accepted

You have to wrap the properties (style attribute) in qoutes ('' or "").

This is because they contain a dash (-).

Working demo

$('p').css({
    'padding-left' : '6px',
    'border-left' : '5px solid #555'
});
share|improve this answer
sweet stuff, thank you. you see, I referred to this post: stackoverflow.com/questions/447197/… and there they do not have width and height in quotes''...I guess it is because of the hyphen... – David Van Staden Mar 28 at 14:07
Right. From api.jquery.com/css: "Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. ... Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name." – dgvid Mar 28 at 14:08
Correct, if you just wanted to add padding (does not contain a dash) instead of padding-left, you could avoid the qoutes. Some however consider it best practice to always wrap in qoutes. That way you always avoid these kind of situations :) – Xeano Mar 28 at 14:09
yip, thanks guys – David Van Staden Mar 28 at 15:59

Just missing quotes

$('p').css({
    'padding-left' : '6px',
    'border-left' : '5px solid #555'
});
share|improve this answer

Because you're not defining them within quotes in the first piece of code.

$('p').css({
    'padding-left' : '6px',
    'border-left' : '5px solid #555'
});

Should work.

share|improve this answer

try

$('p').css({
    'padding-left':'6px',
    'border-left':'5px solid #555'
});
share|improve this answer

Better to use this way:

CHECK THIS OUT IN FIDDLE HERE

$('p').css({
    paddingLeft : '6px',
    borderLeft : '5px solid #555'
});

in css we use with hyphen - separated all lowercase and enclosed in "" single or double quotes. This way in js/jQuery:

    'padding-left' : '6px',
    'border-left' : '5px solid #555'
//--^-----------^----------------------here 

While in js all the css properties are accessible in camelCase this way:

    paddingLeft : '6px',
    borderLeft : '5px solid #555'
//--^^^^^^^^^^------------------------this way
share|improve this answer
cool man, did not know that... – David Van Staden Mar 28 at 14:07
Oh! Great SO welcomes you. – Jai Mar 28 at 14:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.