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>
prepend() method inserts a set of Node objects or string objects before the first child of the Element . String objects are inserted as equivalent Text nodes.
The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.
before() . . append() adds the parameter element inside the selector element's tag at the very end whereas the . after() adds the parameter element after the element's tag.
First, select the ul element by its id by using the querySelector() method. Second, declare an array of strings. Third, for each element in an array, create a new li element with the textContent is assigned to the array element. Finally, prepend the li elements to the ul parent element by using the prepend() method.
Try splitting your code up a bit. It's failing because .css()
is actually being called on the parent and this
is referring to window
in your context.
jsFiddle
$(function(){
// Get all elements matching selector 'li span'
var spans = $("li span");
// Loop for each element in spans
spans.each(function () {
// Create the new element as a jQuery object
var elem = $('<div class="colorBox">test</div>');
// Set the new element's background-color to the attribute on the parent
// of the span
elem.css('background-color', $(this).parent().attr("data-background"));
// Prepend the new element to the span (insert it as the first child)
$(this).prepend(elem);
});
});
If you mean to wrap "Brick Red" in the div
then you will need to use .wrap()
or .wrapInner()
.
jsFiddle
$(this).wrap(elem);
If you're after custom checkboxes, I suggest a more css-driven approach which takes advantage of the <label>
tag.
jsFiddle
HTML
<ul>
<li data-background="#C11B17">
<input type="checkbox" name="color[]" value="Brick_Red" id="checkbox1" />
<label for="checkbox1">
<span>Brick Red</span>
</label>
</li>
</ul>
CSS
input[type=checkbox] {
display:none;
}
input[type=checkbox] + label:before {
display:inline-block;
content:"";
width:1em;
height:1em;
background-color:#CCC;
}
input[type=checkbox]:checked + label:before {
background-color:#C11B17;
}
Note that this method will work in IE8+ without any polyfills.
If your going to use .css() instead of css, then why not just set it using style?
var color = $('li').attr('data-background');
$("span").prepend('<div class="colorBox" style="background:' + color + '"></div>');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With