22

I would like to do this:

<div style="float: left;
            width: 59px;
            background: transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px;"
     class="cIconSmall">
</div>

and I'm thinking I should use this:

$("#YourElementID").css({
    float: "left",
    width: "59px",
    background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"
});

Any ideas?

Thanks

2
  • 1
    Yes, that's the right syntax. Why didn't you just try it? Commented Apr 19, 2011 at 18:21
  • 3
    The only thing wrong with the code you first posted is that instead of $('#YourElementId') , you needed to do $('.YourClassName') since the div has a Class and not an Id Commented Apr 19, 2011 at 18:24

3 Answers 3

48

You're thinking correctly. Using the css(map) method is the way to go.

$(".cIconSmall").css({
    float: "left", 
    width: "59px", 
    background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px" 
});

http://api.jquery.com/css/

A map of property-value pairs to set.


Might be nicer as a css class, though... then you can just write $(".cIconSmall").addClass("icon47"); but there's a time for everything...

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, i was thinking to add a class, but my link will be different every time.
I'm sorry but it doesnot work until i put float ,width,backroung around quotes
I had to do this for it to work. $('#selector').css({'property': 'val','property': 'val'});
2
$(".yourClass").css({
    float: "left",
    width: "59px",
    background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"
});

Comments

-1

I found that Andrew Lohr's comment in the accepted solution is for sure the only way I got this to work in my script: $('#selector').css({'property': 'val','property': 'val'}); (and thanks Andrew).

1 Comment

Comments should be added as a comment, not an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.