Erik
Erik

Reputation: 3

How can I change the background-image of an option while creating it?

I'm creating an option in a select element and trying to change the background image.

nextItem = document.createElement('option');

nextItem.innerHTML = text;

nextItem.style.backgroundImage = "url(icons/add.png);";

nextItem.className = "class1";

When I use firebug, however, I can see that what actually gets created is this:

<option class="class1" style="">text</option>

Why is it creating the the style attribute but not putting any information in it?

Upvotes: 0

Views: 149

Answers (2)

j08691
j08691

Reputation: 207901

Remove the semi-colon in the url.

Change:

nextItem.style.backgroundImage = "url(icons/add.png);";

to:

nextItem.style.backgroundImage = "url(icons/add.png)";

jsFiddle example

Upvotes: 2

M Abbas
M Abbas

Reputation: 6479

You would try this nextItem.style.backgroundImage = "url('icons/add.png')";

Upvotes: 1

Related Questions