By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

How to compare two Ajax elements

P: 2
Ok so I'm trying to monitor and control something with a Pic-web from microchip. I managed to program it all except for one thing. I need to compare two temperatures that it gives me, and if one of them is smaller that the other then a relay need to be activated, if its bigger or equal then another relay must be activated. From what I understood Pic-web updates an xml file (status.xml) then the website (using ajax) takes the values from there and displays them on the web page.

This is the code in my xml file
Expand|Select|Wrap|Line Numbers
  1. <temp0>~temp~</temp0>
  2. <temp2>~tempc~</temp2>
This is the code for the temperatures
Expand|Select|Wrap|Line Numbers
  1.     document.getElementById('temp0').innerHTML = getXMLValue(xmlData, 'temp0');
  2.     document.getElementById('temp2').innerHTML = getXMLValue(xmlData, 'temp2');
And this is the code I use to display them in my web page
Expand|Select|Wrap|Line Numbers
  1. <span id="temp0"style="font-weight:normal">?</span>
  2. <span id="temp2"style="font-weight:normal">?</span>
I have almost 0 knoledge of ajax
I tried to compare them with

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. if(temp2==temp0) document.write("On".fontsize(4).fontcolor("green"));
  3. else document.write("Off".fontsize(4).fontcolor("red"));
  4. </script>
but it just displays OFF
Jun 19 '12 #1

✓ answered by Sorinpass

found the answer if anyone is interested

Expand|Select|Wrap|Line Numbers
  1.     var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
  2.     var temp2 = parseFloat(document.getElementById('temp2').innerHTML)
  3.  
  4.     if( temp0 < temp2 ) {
  5.         document.getElementById('temp3').innerHTML = '<font color="#00FF00">ON</font>';
  6.     } else {
  7.         document.getElementById('temp3').innerHTML = '<font color="#FF0000">OFF</font>';
  8.     }
This will display ON if temp2 > temp0

and OFF if temp0 < temp2

Share this Question
Share on Google+
2 Replies


P: 2
found the answer if anyone is interested

Expand|Select|Wrap|Line Numbers
  1.     var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
  2.     var temp2 = parseFloat(document.getElementById('temp2').innerHTML)
  3.  
  4.     if( temp0 < temp2 ) {
  5.         document.getElementById('temp3').innerHTML = '<font color="#00FF00">ON</font>';
  6.     } else {
  7.         document.getElementById('temp3').innerHTML = '<font color="#FF0000">OFF</font>';
  8.     }
This will display ON if temp2 > temp0

and OFF if temp0 < temp2
Jun 19 '12 #2

acoder
Expert Mod 15k+
P: 16,027
Try to avoid using <font> tags - they're deprecated. Try using CSS classes, e.g.
Expand|Select|Wrap|Line Numbers
  1. .on {
  2.     color: #00FF00;
  3. }
and in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <span id="temp3" class="on">ON</span>
To set the class with javascript:
Expand|Select|Wrap|Line Numbers
  1. temp3.className = "on";
Jun 20 '12 #3

Post your reply

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