Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set background image of a css element

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??

like image 304
Rony Avatar asked Jul 25 '10 13:07

Rony


3 Answers

$(cell).css("background-image","url('mybg.png)");
like image 181
SirDemon Avatar answered Oct 14 '22 00:10

SirDemon


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.

like image 39
p4bl0 Avatar answered Oct 13 '22 23:10

p4bl0


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');
like image 22
karim79 Avatar answered Oct 13 '22 22:10

karim79