0

I have a HTML form where the variables are pulled from JSON. The variable can be “In Progress”, “Pending” or “Completed”.

Is it possible to keep the background of Form based on variable?

The HTML code is below:

$(function() {
    $.getJSON('C:/GChartServlet/end.json', function(statusDataSet) {
        $.each(statusDataSet.statuses, function(i, f) {
            if(f.name=="Autogen Program") {
                $("#end1").append(f.value);
            }
            else if(f.name=="Auto Approval Program") {
                $("#end2").append(f.value);
            }
            else if(f.name=="Time Transfer from OTL to BEE") {
                $("#end3").append(f.value);
            }
        });
    });
});

I was trying to keep the form background or even value in different color depending on value it pulls from JSON.

I am new to HTML coding. Any help is appreciated.

Regards.

1 Answer 1

3
$.getJSON('C:/GChartServlet/end.json', function(statusDataSet) {
    $.each(statusDataSet.statuses, function(i, f) {
        var color;
        switch(f.status) {
            case "In Progress":
                color = "yellow";
                break;
            case "Pending": 
                color = "red";
                break;
            case "Completed": 
                color = "green";
                break;
        }
        if(f.name=="Autogen Program") {
            $("#end1").append(f.value).css('background-color', color);
        }  else if(f.name=="Auto Approval Program") {
            $("#end2").append(f.value).css('background-color', color);
        } else if(f.name=="Time Transfer from OTL to BEE") {
            $("#end3").append(f.value).css('background-color', color);
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

@Barmar .. Thanks again for your help but when I try to use it I keep getting error Uncaught SyntaxError: Unexpected string on line 17 . The line 17 is " switch(f.value) { "In Progress": "
Oops, I left out the case keywords in the switch block. Fixed now.

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.