This prepend
function adds a div
with the "colorBox" class, but I'm having trouble setting the css for the newly created div
. I don't know if my syntax is quite right, but I'm trying to use the data-background
value in the parent (li
) tag.
I'm using this to add color boxes to multiselect
options
, and the plugin that I'm using converts each option
into a <li>
that is structured like the HTML I've included below.
JS
$(function(){
$("li span").prepend('<div class="colorBox"></div>').css('background-color', $(this).parent().attr("data-background"));
});
HTML
<ul>
<li data-background="#C11B17">
<input type="checkbox" name="color[]" value="Brick_Red">
<span>Brick Red</span>
</li>
</ul>