6

i use the following to to set the text content of an css element

var cell = document.createElement('li');
        cell.textContent = this.labelForIndex(index);

Now i want to set the back-ground image and color.....how to do it??

4 Answers 4

4
$(cell).css("background-image","url('mybg.png)");
Sign up to request clarification or add additional context in comments.

2 Comments

ok...sorry.....i am new in this site so don't actually know much about it....i tried to put tick mark but i can only tick one..but many developer give me right ans...
You can only have one accepted answer per question. If you feel one answer is better than the one you've already accepted, you can change your accepted answer.
3

You can use the "map version" of jQuery css method:

$(cell).css({'background-image': 'url("your-bg.png")',
             'background-color': '#abcdef'});

But if you can (if background image and color is always the same for instance) use addClass as karim79 told you to.

2 Comments

just a thought, but perhaps "...as Karim79 suggested..." would sound a little gentler?
@ricebowl: ah, yea. I knew my formulation was a bit rude but nothing else came up to my mind when writing the post, sorry :-).
3

Use addClass. I think it is less verbose, more efficient, and prettier, plus it is better (i.e. more maintainable) to keep your style definitions outside of your implementation:

.prettyWithImage { background-image: url(/someimage.jpg); color: red }

$(cell).addClass('prettyWithImage');

Comments

2

Using jQuery:

$(cell).css('background-image', 'url(image-location.jpg)').css('color', '#ABCDEF');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.