1

Trying to learn more about jQuery I have run into an issue with trying to display an inline CSS into a <span>. I am trying to take an array and display it as followed:

<div class="colorbar">
    <span style="background: #A56AC2"></span>
    <span style="background: #E2CED7"></span>
    <span style="background: #5E5992"></span>
    <span style="background: #E4E6A2"></span>
    <span style="background: #B45748"></span>
</div><!-- end .colorbar -->

My array:

var colorArray = new Array(
    "A56AC2",
    "E2CED7",
    "5E5992",
    "B45748",
    "E4E6A2"
);

Ive tried:

 $('.colorbar').html( '<span style="background: #' + colorArray. + (';</span>'));

From this article I assumed:

$('.colorbar').html( '<span style="background: #' + colorArray.join + (';</span>'));

and:

var addingHTML = [];
for ( var i=0; i < colorArray.length; i++ ) {
    addingHTML.push ('<span style="background: #' + colorArray + ';"');
}
$(".colorbar").html(addingHTML.join(""));

Ive looked into:

  • adding .css() to the <span> but going through the array Im having trouble.
  • using .each()

However, it is not rendering correctly. Does someone mind educating me on what I am doing wrong?

1
  • Your third try with the for loop was almost correct. It should be colorArray[i] to access just the item at index i. Commented Apr 22, 2014 at 14:59

4 Answers 4

2

DEMO -> http://jsfiddle.net/JBSKL/1/

By using your code..

var colorArray = new Array("A56AC2", "E2CED7", "5E5992", "B45748", "E4E6A2");

var addingHTML = [];
for ( var i=0; i < colorArray.length; i++ ) {
    addingHTML.push ('<span style="background: #' + colorArray[i] + '">' + i + '</span>');
}
$(".colorbar").html(addingHTML.join(""));

Few issues fixed

  1. In your array, you have an extra comma , at the end
  2. Use colorArray[i] instead of colorArray in the for loop
  3. Close span tag in your for loop.
Sign up to request clarification or add additional context in comments.

2 Comments

+1 I didnt see the extra , at the end of the array()
You don't need a comma after this "E4E6A2",
1

Try this:

$(colorArray).each(function(i, e) {
    $('.colorbar').append('<span style="background: #' + e + '"></span>');
});

The each(index, element)-function iterates all elements of a object, in your case your array colorArray.

In my example i is my name for the first argument, index and just contains the current position, starting at 0.

The second argument is element (or short e, in my example) and contains the current element of your object/list/whatever. In your case: your color codes.

2 Comments

Thanks for the help! Do you mind explaining what the e does?
I updated my answer and hope you can understand it better, now. If not, just ask! :)
1

Perhaps a cleaner approach would be to use the built-in css function. Also, you should build your collection of spans then append them all at once to the DOM, minimizing the number of DOM manipulations:

var colorArray = [
    "A56AC2",
    "E2CED7",
    "5E5992",
    "B45748",
    "E4E6A2"
];

$(function () {
  var $spans = new $();
  $.each(colorArray, function(i, e) {
      $spans = $spans.add($("<span>Howdy</span>").css("background-color", "#" + e));
  });

  $(".colorbar").append($spans);
});

Here the Codepen.

Comments

0

Life can be much easier without jQuery most of the time.

 var colorArray = ["A56AC2", "E2CED7", "5E5992", "B45748", "E4E6A2"];
 colorArray.forEach(color => {
     document.querySelector('.colorbar').innerHTML += '<span style="background: #' + color + '"></span>';
 });

Otherwise, with jQuery:

 var colorArray = ["A56AC2", "E2CED7", "5E5992", "B45748", "E4E6A2"];
 colorArray.forEach(color => {
      $('.colorbar').html($('.colorbar').html() + '<span style="background: #' + color + '"></span>');
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.