Home > DeveloperSection > Forums > How can I set different class for div cheking the json data
Aaron Douglas
Aaron Douglas

Total Post:20

Posted on    June-20-2013 3:12 AM


 1 Reply(s)
 885  View(s)
Rate this:
Hi Mindstickians,

How can I check true or false from json and put different css for div if true or false:

For example json:

    "Object": [
        "test": true,
        "number": "001"
    }, {
        "test": false,
        "number": "002"
    }, {
        "test": false,
        "number": "003"
    }, {
        "test": true,
        "number": "004"

and i created html:

<div class="test" id="001">001</div>
<div class="test" id="002">002</div>
<div class="test" id="003">003</div>
<div class="test" id="004">004</div>

I tried something like this but it is wrong...

if (test == 'true') {
$(".test").css("background-color", "blue");
} else {
$(".test").css("background-color", "red");

Thanks in advance for any recommendations or solutions.


Total Post:604

Posted on    June-20-2013 7:34 AM


For checking true or false from json try as following

$.each(json.Object, function(index, value) {
    $number = $("#"+value.number);
    if(value.test) $number.css("background-color", "blue");
    else $number.css("background-color", "red");

another way as following 

$.each(json.Object, function(index, value) {
    $("#"+value.number).css("background-color", (value.test ? "blue" : "red"));

I hope it helpful for you.

Don't want to miss updates? Please click the below button!

Follow MindStick