0

I have 4 divs and I have to apply the background for every of them according to this array of objects. I know the method css() but didn't know how to loop work.

[
  {
    "tix_type": "adult",
    "bg": "gold",
    "tix_pax": 1
  },
  {
    "tix_type": "child",
    "bg": "brown",
    "tix_pax": 0
  },
  {
    "tix_type": "senior",
    "bg": "red",
    "tix_pax": 2
  },
  {
    "tix_type": "disabled",
    "bg": "green",
    "tix_pax": 1
  }
]

There is no need for sequence, like 1 div is green, gold and 2 divs are red.

3
  • Can you add your html? Commented Mar 28, 2016 at 12:04
  • loop would have been helpful if your inner objects also had some identifier like DIV ID to identify corresponding DIV's.. you can use jQuery('firstDIvID').css('background': arr[0]['bg']);... so on for other div's Commented Mar 28, 2016 at 12:14
  • jsfiddle.net/arunpjohny/fag3w0xj/1 Commented Mar 28, 2016 at 12:16

4 Answers 4

2

Since you haven't provided the HTML structure, This would help you understand the basic logic. you might have to change the part $('div.styleMe:eq(' + i + ')') to your respective selctors. Let me know if this helps.

var config = [{
  "tix_type": "adult",
  "bg": "gold",
  "tix_pax": 1
}, {
  "tix_type": "child",
  "bg": "brown",
  "tix_pax": 0
}, {
  "tix_type": "senior",
  "bg": "red",
  "tix_pax": 2
}, {
  "tix_type": "disabled",
  "bg": "green",
  "tix_pax": 1
}]

$.each(config,function(i, v) {
  $('div.styleMe:eq(' + i + ')').css('background-color', v.bg);
});
div {
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="styleMe"></div>
<div class="styleMe"></div>
<div class="styleMe"></div>
<div class="styleMe"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

this is wrong, brown should not be shown as the pax is zero, there should be 2 red.
0
var styleArray = [
  {
    "tix_type": "adult",
    "bg": "gold",
    "tix_pax": 1
  },
  {
    "tix_type": "child",
    "bg": "brown",
    "tix_pax": 0
  },
  {
    "tix_type": "senior",
    "bg": "red",
    "tix_pax": 2
  },
  {
    "tix_type": "disabled",
    "bg": "green",
    "tix_pax": 1
  }
];

$(document).ready(function(){
    $(".styled-divs").each(function(index){
       $(this).css("background-color", stylesArray[index].bg );
    });
});

HTML CODE

<div class="styled-divs"></div>
<div class="styled-divs"></div>
<div class="styled-divs"></div>
<div class="styled-divs"></div>

Comments

0

If you have a common class(or any selector) to select them all then

var array = [{
  "tix_type": "adult",
  "bg": "gold",
  "tix_pax": 1
}, {
  "tix_type": "child",
  "bg": "brown",
  "tix_pax": 0
}, {
  "tix_type": "senior",
  "bg": "red",
  "tix_pax": 2
}, {
  "tix_type": "disabled",
  "bg": "green",
  "tix_pax": 1
}];

$('.target').css('background-color', function(i) {
  return array[i].bg;
});
.target {
  margin-bottom: 5px;
  min-height: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="target"></div>
<div class="target"></div>
<div class="target"></div>
<div class="target"></div>

1 Comment

this is wrong, brown should not be shown as the pax is zero, there should be 2 red.
0

Try the jQuery css function directly.

var arrayOfObjRef = [
  {
    "tix_type": "adult",
    "bg": "gold",
    "tix_pax": 1
  },
  {
    "tix_type": "child",
    "bg": "brown",
    "tix_pax": 0
  },
  {
    "tix_type": "senior",
    "bg": "red",
    "tix_pax": 2
  },
  {
    "tix_type": "disabled",
    "bg": "green",
    "tix_pax": 1
  }
];

$('div.content').css('background-color', function(i) {
   return arrayOfObjRef[i].bg;
});

console.log('First object bg: ' + arrayOfObjRef[0].bg);
console.log('second object tix_type: ' + arrayOfObjRef[1].tix_type);
console.log('third object tix_pax: ' + arrayOfObjRef[2].tix_pax);
console.log('third object bg: ' + arrayOfObjRef[3].bg);
.content {
  height:20px;
  margin:5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>

3 Comments

this is wrong, brown should not be shown as the pax is zero, there should be 2 red.
Where did you mention to have the dependency on pax?
There is no need for sequence, like 1 div is green, gold and 2 divs are red.

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.