473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to turn text red if negative or green if positive?

Hello all, I have the following code for a form that calculates the total amount based on some form elements, how would I go about changing the text color in the div called total to green if it's a positive number or red if it's a negative number? Thanks in advance for any help or suggestions!

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Our hosting plans are not only affordable, but they're packed full of features, it's an increadible deal!</title>
  6. <link href="style.css" rel="stylesheet" type="text/css"></link>
  7. <style>
  8. .test {
  9.     color:red;
  10. }
  11. #total {
  12.     font-size:40px;
  13. }
  14. h1 {
  15.     margin: 0 0 10px 0;
  16.     text-decoration: underline;
  17.     text-align: center;
  18. }
  19. h2 {
  20.     margin: 0 0 10px 0;
  21. }
  22. .wrapr form ul {
  23.     float: left;
  24.     width: 300px;
  25.     list-style-type: none;
  26.     padding: 0px;
  27.     margin-top: 0px;
  28.     margin-right: 0px;
  29.     margin-bottom: 0px;
  30.     margin-left: 10px;
  31. }
  32. .wrapr form ul li {
  33.     display: inline;
  34.     float: left;
  35.     margin-right: 15px;
  36. }
  37. </style>
  38. <script>
  39.   calculate = function(totalElement)
  40.   {
  41.       if (totalElement)
  42.       {   
  43.           var calculation  = '';
  44.           var overall = '';
  45.           var fields = new Array();
  46.           var theElement = document.getElementById(totalElement);
  47.           var userInputs = myform.elements;
  48.           var the_type = '';
  49.           for (var f = 0; f < userInputs.length; f++)
  50.           { 
  51.               if (userInputs[f].className=='special_value')
  52.               {
  53.                   if (userInputs[f].type=='select-one')
  54.                   {
  55.                       if(userInputs[f].options[userInputs[f].selectedIndex].value)
  56.                       {
  57.                           fields[f] = userInputs[f].options[userInputs[f].selectedIndex].value;
  58.                       }
  59.                       else
  60.                       {
  61.                           fields[f] = 0;
  62.                       }
  63.                   }
  64.                   else if(userInputs[f].type=='radio' || userInputs[f].type=='checkbox')
  65.                   {
  66.                       if (userInputs[f].checked)
  67.                       {
  68.                           fields[f] = userInputs[f].value;
  69.                       }
  70.                       else
  71.                       {
  72.                           fields[f] = 0;
  73.                       }
  74.                   }
  75.                   else
  76.                   {
  77.                       if (userInputs[f].value)
  78.                       {
  79.                           fields[f] = userInputs[f].value;
  80.                       }
  81.                       else
  82.                       {
  83.                           fields[f] = 0;
  84.                       }
  85.                   }
  86.               }
  87.           }
  88.  
  89.           for (var i=0; i<fields.length; i++)
  90.           { 
  91.               calculation += fields[i];
  92.  
  93.               if (i!=fields.length-1)
  94.               {
  95.                   calculation += '+';
  96.               }
  97.           }
  98.  
  99.           if (calculation!='')
  100.           {
  101.               overall = eval(calculation).toFixed(2);
  102.           }
  103.  
  104.           if (overall!='')
  105.           {
  106.               theElement.innerHTML = 'Your quote is within $'+overall+ ' of your budget';
  107.           }
  108.       }
  109.   }
  110. </script>
  111.  
  112. <!--[if IE]>
  113.     <script src="lib/html5.js" type="text/javascript"></script>
  114. <![endif]-->
  115. <script type="text/javascript">
  116.  
  117.   var _gaq = _gaq || [];
  118.   _gaq.push(['_setAccount', 'UA-16023202-1']);
  119.   _gaq.push(['_trackPageview']);
  120.  
  121.   (function() {
  122.     var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  123.     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  124.     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  125.   })();
  126.  
  127. </script>
  128. </head>
  129.  
  130. <body>
  131. <div class="header">
  132.   <div class="top"><script type="text/javascript" src="/help/livehelp_js.php?eo=1&relative=Y&amp;department=2&amp;serversession=1&amp;pingtimes=15&amp;secure=Y"></script></div>
  133.   <div class="logo">
  134.     <h1><a href="http://www.drummerboyhosting.com/">drummer boy hosting web design and hosting servies</a></h1>
  135.   </div>
  136.   <div class="menuwrap">
  137.   <ul id="topnav"> 
  138.         <li> 
  139.             <a href="index.php" class="home">Home Page</a> 
  140.         </li> 
  141.         <li> 
  142.              <a href="plans.php" class="products">Hosting Packages</a> </li>
  143.       <li><a href="portfolio.php">Design Portfolio</a></li>
  144.       <li class="current"><a href="quotes.php">Design Quotes</a></li>
  145.         <li><a href="contact.php">Contact Us</a></li>
  146.   </ul> 
  147.   </div>
  148. </div>
  149. <div class="wrapr">
  150. <form name="myform">
  151.  
  152.   <h2>Please fill out the form below to obtain your quote!</h2>
  153.   <p>Do you need a cms (content management system)?<br />
  154.     <select onchange="calculate('total');" name="price" class="special_value">
  155.       <option value="0">Please Choose One</option>
  156.       <option value="-150">No. A static website will do</option>
  157.       <option value="-250">Yes, WordPress</option>
  158.       <option value="-499">Yes, Joomla</option>
  159.       <option value="-499">Yes, Drupal</option>
  160.     </select>
  161.   </p>
  162.   <p>What is your budget?<br />
  163.     <select onchange="calculate('total');" name="price5" class="special_value">
  164.       <option value="0">Please Choose One</option>
  165.       <option value="700">$700</option>
  166.       <option value="800">$800</option>
  167.       <option value="900">$900</option>
  168.       <option value="1000">$1000</option>
  169.       <option value="1100">$1100</option>
  170.       <option value="1200">$1200</option>
  171.       <option value="1300">$1300</option>
  172.       <option value="1400">$1400</option>
  173.       <option value="1500">$1500</option>
  174.       <option value="1600">$1600</option>
  175.       <option value="1700">$1700</option>
  176.       <option value="1800">$1800</option>
  177.       <option value="1900">$1900</option>
  178.       <option value="2000">$2000</option>
  179.     </select>
  180.     <br /><br /><br />
  181.   </p>
  182.   <div id="total"></div>
  183.  
  184.   <input onClick="calculate('total');" type="button" name="button" value="re-calculate">
  185.  
  186. </form>
  187. <br style="clear:both" />
  188. </div>
  189. <div class="planfeatures"></div>
  190. <div class="footer">  <ul id="fnav">
  191.       <li> <a href="index.php" class="home">Home Page</a> </li>
  192.       <li > <a href="plans.php" class="products">Hosting Packages</a></li>
  193.       <li><a href="portfolio.php">Design Portfolio</a></li>
  194.       <li class="fcurrent"><a href="quotes.php">Design Quotes</a></li>
  195.       <li><a href="contact.php">Contact Us</a></li>
  196.     </ul></div>
  197. <!--[if IE 8]>
  198.     <script type="text/javascript">
  199.         $("#griddler_ii section ul li:nth-child(even)").addClass("even");
  200.     </script>
  201. <![endif]-->
  202. </body>
  203. </html>
Nov 10 '10 #1
4 4495
NeoPa
32,556 Expert Mod 16PB
I can't help with an answer, but if I suggest that you indicate whereabouts in your lengthy posted code the relevant item can be found, it may help you to get one from someone who knows this subject.
Nov 10 '10 #2
Thank you NeoPa, I want the item text in the div id of total on line 182 which is generated from the javascript on lines 104 to 107.
Nov 10 '10 #3
NeoPa
32,556 Expert Mod 16PB
As I say, I've never used JavaScript, but until a local expert turns up I would suggest setting up a variable (around line #48 or so) that you can set up with the string of the HTML tag for the colour.

Your code, inserted before line #106, would assign the correct value for either green or red, depending on an If statement that checks either the overall or calculation values against zero (0).

Lastly, you would update the line that is currently #106 (but has been shifted down with the addition of the earlier inserted code), to include this new variable, and thus the already correct HTML code, such that when resolved it includes your value as coloured in the result of theElement.innerHTML.

Does this make sense to you?
Nov 11 '10 #4
Thanks again NeoPa but not at all, I was given this code by someone that created it and said call me if you need anything else, and vioala, now that he has my money and forgot that I wanted this feature he is unreachable. Thank you though! I appreciate your attempt at trying to help me, but if you could provide me with an example code, I would really appreciate it! Thanks again!
Nov 15 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Xah Lee | last post by:
i have a large number of lines i want to turn into a list. In perl, i can do @corenames=qw( rb_basic_islamic sq1_pentagonTile sq_arc501Tile sq_arc503Tile );
2
by: bafidi | last post by:
i write a number to textbox1.text i want to learn if it is negative or positive
12
by: deko | last post by:
Is there an easy way to make all negative values positive in a particular table? I've been experimenting with this: Dim rst As DAO.Recordset Set rst = db.OpenRecordset("tblNegValues") Do...
19
by: Johs | last post by:
I need to make some special action if 'a' and 'b' are both positive or both negative. Is there some inbuilt function to check this? Johs
2
by: STEPHENSS | last post by:
Hi, New to SQL. I need to reverse the signs, for example I have an amount of -1,500.00 and I need to change the the Amount to 1,500.00 and all positives amount to a negative amount also. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.