/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Simple if function not working as expected

I am new to javascript and am trying to do something that should be trivial but I am having difficulties. I have a javacript function that returns ad json array from a PHP backend database. I then extract this data and put it into different text boxes on my page. This part works just fine. for instance, the following code puts either a value of enabled or disabled into a textbox with an id of status.

[CODE]$(“#status”).val(result[‘gtt_account_status’]);[/CODE]

However, I wish to change the color of the text box depending on the value that is returned. So I tried many variations of the following:

[CODE]If ((document.getElementById(“status”).value) == “enabled”){
document.getElementById(“status”).style.backgroundColor = “lightgreen”;
}[/CODE]

As soon as I add this bit of code to my function, the data no longer gets put into the textbox at all. If I remove the if function and only add the

[CODE]document.getElementById(“status”).style.backgroundColor = “lightgreen”;[/CODE]

then the text box tuns light green like it should. I just cant seem to put this all together. below is the entire function.

[CODE]function insertResults(result){
$(“#status”).val(result[‘gtt_account_status’]);
If ((document.getElementById(“status”).value) == “enabled”){
document.getElementById(“status”).style.backgroundColor = “lightgreen”;
}
}[/CODE]

Any help would be most appreciated.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@pmontalbanoauthorJan 23.2015 — I was able to resolve this issue with the following.

[CODE]
function insertResults(result){
if (result["gtt_account_status"] == "Disabled") {
$("#status").val(result['gtt_account_status']);
document.getElementById("status").style.backgroundColor = "red";
} else {
$("#status").val("Enabled");
document.getElementById("status").style.backgroundColor = "lightgreen";
}
}
[/CODE]
×

Success!

Help @pmontalbano spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...