Set background image of css element

i to set the text content of the css element

use the following:
var cell = document.createElement('li'); cell.textContent = this.labelForIndex(index); 

Now I want to set the background image and color ..... how to do it?

+6
javascript jquery css
source share
4 answers
 $(cell).css("background-image","url('mybg.png)"); 
+4
source share

Use addClass . I think it is less detailed, more efficient and beautiful, plus better (i.e., more convenient to maintain) so that your style definitions are outside your implementation:

 .prettyWithImage { background-image: url(/someimage.jpg); color: red } $(cell).addClass('prettyWithImage'); 
+3
source share

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

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

But if you can (if the background image and color are always the same), use addClass , as karim79 told you.

+3
source share

Using jQuery:

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

All Articles