How to compare two Ajax elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sorinpass
    New Member
    • Jun 2012
    • 2

    How to compare two Ajax elements

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

    Code:
    <script type="text/javascript">
    if(temp2==temp0) document.write("On".fontsize(4).fontcolor("green"));
    else document.write("Off".fontsize(4).fontcolor("red"));
    </script>
    but it just displays OFF
    Last edited by acoder; Jun 20 '12, 09:40 AM. Reason: Added [code] tags
  • Sorinpass
    New Member
    • Jun 2012
    • 2

    #2
    found the answer if anyone is interested

    Code:
    	var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
    	var temp2 = parseFloat(document.getElementById('temp2').innerHTML)
    
        if( temp0 < temp2 ) {
        	document.getElementById('temp3').innerHTML = '<font color="#00FF00">ON</font>';
        } else {
        	document.getElementById('temp3').innerHTML = '<font color="#FF0000">OFF</font>';
        }
    This will display ON if temp2 > temp0

    and OFF if temp0 < temp2
    Last edited by acoder; Jun 20 '12, 09:40 AM. Reason: Please use [code] tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Try to avoid using <font> tags - they're deprecated. Try using CSS classes, e.g.
      Code:
      .on {
          color: #00FF00;
      }
      and in the HTML:
      Code:
      <span id="temp3" class="on">ON</span>
      To set the class with JavaScript:
      Code:
      temp3.className = "on";

      Comment

      Working...