HOW CAN I SET DIFFERENT CLASS FOR DIV CHEKING THE JSON DATA

Aaron Douglas

Total Post:20

Points:140
Posted by  Aaron Douglas
JSON 
 1182  View(s)
Ratings:
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.
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How can I set different class for div cheking the json data

    Hi,

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!