/    Sign up×
Community /Pin to ProfileBookmark

help can’t figure why the color won’t change

Having trouble trying to get the text color to change. The value is negative (-100%).

Example
-130 = green
-131 to -140 = yellow
-140+ = red

[CODE]
function NetPercent()
{
var callsnum = document.getElementById(“TotalCalls”).value;
var val = Math.round(((upgrades – downgrades)/callsnum)*100);
document.getElementById(“NetPercent”).value = val + “%”;

var net = document.getElementById(“NetPercent”);
if ( net >= -.13 )
net.style.color = ’00ff00′;
if ( net < -0.131 && net >= -0.14 )
net.style.color = ‘ffff00’;
if ( net < -0.141 )
net.style.color = ‘ff0000’;

<input type=”text” class=”text” name=”” id=”NetPercent” size=”8″ ReadOnly style=”color:white;”>

[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarMar 26.2011 — Replace [b]net[/b] with [b]val[/b] in your if statements.
Copy linkTweet thisAlerts:
@MrSnowDropMar 26.2011 — You can correctly use "net" variable to set style colors because it's declared this way:

[CODE]var net = document.getElementById("NetPercent");[/CODE]

But you can't use the same variable to read values, so simply declare another variable:

[CODE]var [COLOR="darkred"]netval[/COLOR] = document.getElementById("NetPercent")[COLOR="DarkRed"].value[/COLOR];
if ( netval >= -.13 ) ...[/CODE]
Copy linkTweet thisAlerts:
@scragarMar 26.2011 — You can correctly use "net" variable to set style colors because it's declared this way:

[CODE]var net = document.getElementById("NetPercent");[/CODE]

But you can't use the same variable to read values, so simply declare another variable:

[CODE]var [COLOR="darkred"]netval[/COLOR] = document.getElementById("NetPercent")[COLOR="DarkRed"].value[/COLOR];
if ( netval >= -.13 ) ...[/CODE]
[/QUOTE]


He already has the value in the variable [b]val[/b], and even if he didn't the assignment he makes puts a % at the end meaning it returns a string, not a number.
Copy linkTweet thisAlerts:
@DJScriptauthorMar 26.2011 — i did the change from net to val before still got a error with it..

line 103

char 12

'style' is null or not a object

[CODE]
100
101(w & w/o)// var net = document.getElementById("NetPercent");
102 if ( val >= -.13 )
103 val.style.color = '00ff00';
104 if ( val < -0.131 && net >= -0.14 )
105 val.style.color = 'ffff00';
106 if ( val < -0.141 )
107 val.style.color = 'ff0000';
108
[/CODE]
Copy linkTweet thisAlerts:
@scragarMar 26.2011 — Sorry for saying it badly before. Use val in your comparisons and net when setting the colour.

<i>
</i>if ( val &gt;= -13 )
net.style.color = '00ff00';
if ( val &lt; -13 &amp;&amp; val &gt;= -14 )
net.style.color = 'ffff00';
if ( val &lt; -14 )
net.style.color = 'ff0000';
Copy linkTweet thisAlerts:
@MrSnowDropMar 26.2011 — Sorry to all, but the "val" variable calculates something by the "math.round..." operation, and i thought the need was only to read the element's value.

That's the reason i thought to assign another variable, which simply reads the element's value (as a number) :o
Copy linkTweet thisAlerts:
@scragarMar 26.2011 — Sorry to all, but the "val" variable calculates something by the "math.round..." operation, and i thought the need was only to read the element's value.

That's the reason i thought to assign another variable, which simply reads the element's value (as a number) :o[/QUOTE]


TBH I thought the same as you did, but when I went back to check variable and tag IDs I realised what he was actually doing and how easy it actually was to fix(your code actually better matches the description, but not the code).
×

Success!

Help @DJScript 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...