-1

Right now in my AJAX call I send a json array and, I'm getting a json encoded array back from the server. This is functioning correctly, as I can verify the values in dev tools. What I want to add is a conditional block to check the value of response.remaining. If the value is a negative number, then display it one way, if its positive, display it another way, and if else, just display $0.00. Something like this:

request.done(function(response){
    if(response.remaining.value() < 0){
         $('#remaining').html('<h1 class="negativeNum">&#36;' + response.remaining + '</h1>');
    } else if(response.remaining.value() > 0) {
        $('#remaining').html('<h1 class="positiveNum">&#36;' + response.remaining + '</h1>');
    } else {
        $('#remaining').html('<h1>&#36;0.00</h1>');
    };
});
hyphen
  • 957
  • 1
  • 11
  • 31
  • 1
    Please show your JSON response, otherwise it would be hard to figure it out; anyways if you have `{ response : { remaining: someVal }}`, `response.remaining` will do. – moonwave99 Dec 13 '13 at 18:26
  • response.remaining.value() , this seems to be wrong, i guess it should be as @moonwave99 suggests – adi rohan Dec 13 '13 at 18:27
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Siamak Motlagh Dec 13 '13 at 18:30
  • I may have to go back and look at response.remaining.value(). It wasn't working when I tried it before, but maybe something else is going on. – hyphen Dec 13 '13 at 18:33
  • this is my json response: {"income":"5,713.75","expense":"555.00","remaining":"5,158.75","perecnt":"0.097134106322468"} – hyphen Dec 14 '13 at 01:47
  • i'm trying to access the value of "remaining" and compare it to 0. – hyphen Dec 14 '13 at 01:48

1 Answers1

0

This is what ultimately worked for me, and honestly, the link marked as duplicate really didn't help me at all. Maybe this one will help someone else in the future.

request.done(function(response) {
    var remaining = response.remaining;
    if(remaining > "0") {
        $('#cash_remaining').html('<h1 class="positiveNum float-right">&#36;' + response.remaining + '</h1>');
    } else if(remaining < "0") {
        $('#cash_remaining').html('<h1 class="negativeNum float-right">&#36;(' + response.remaining + ')</h1>');
    } else {
        $('#cash_remaining').html('<h1 class="float-right">&#36;0.00</h1>');
    };
});
hyphen
  • 957
  • 1
  • 11
  • 31