views:

122

answers:

2

Hi. Programmers here have been extremely helpful in resolving issues for me in the past, so I thought I'd ask an Ajax question. It's probably a simple fix, but I'm new to Ajax.

What I'd like to do is change the style of the responseText to red if the result is the pharase, "NOT FOUND". Otherwise the text will be black. Here is the script I'm using:

 <script type="text/javascript">
  <!-- 
  function newXMLHttpRequest() 
  {
     var xmlreq = false;
     if (window.XMLHttpRequest) 
     {
        xmlreq = new XMLHttpRequest();
     } 
        else if (window.ActiveXObject) 
     {
        try 
        {
           xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
        }
           catch (e2) 
           {
              alert("Error: Unable to create an XMLHttpRequest.");
           }
      }
        return xmlreq;
  }
  function getLocation(location) 
  {
     var getLocation= newXMLHttpRequest(); // sending request
     getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME&ROUTINGNUM=" + location, false);
     getLocation.send(null); // getting location
     document.getElementById("location_div").innerHTML = getLocation.responseText;
  }
  //-->
 </script>

Here is the HTML:

...
<table>
    <tr>
        <td class="ajax_msg">
         <div id="location_div"><div>       
        </td>
    </tr>
</table>
...
+1  A: 

You should use css for those styles:

JS:

if (reponseText == "NOT_FOUND") 
   document.getElementById("location_div").className = "error";
else
   document.getElementById("location_div").className = "success";

CSS:

.error
{
  color: red;
}
.success
{
  color: black;
}

Edit: Corrected .class to .className (this would be easier with jquery)

gustavogb
Tried that and it did not work. The NOT FOUND message remains black. Added the CSS to my external style sheet exactly as shown in your reply.
Spockrates
Here's what I have for the JS: function getLocation(locationrouting) { var getLocation= newXMLHttpRequest(); // sending request getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME getLocation.send(null); // getting location document.getElementById("location_div").innerHTML = getLocation.responseText; if (reponseText == "NOT FOUND") document.getElementById("location_div").class = "error"; else document.getElementById("location_div").class = "success"; }
Spockrates
Here is he HTML: <tr> <td class="ajax_msg"> <div id="location_div"></div> </td>
Spockrates
Here is the CSS:div.error { color: red;}div.success{ color: black;}td.ajax_msg { padding-left:160px; text-align:bottom; line-height:10px; font-weight:bold;}
Spockrates
Sorry. Typo. Here's the JS: function getLocation(location) { var getLocation= newXMLHttpRequest(); // sending request getLocation.open("GET", "/PP?PAGE=GETLOCATIONNAME getLocation.send(null); // getting location document.getElementById("location_div").innerHTML = getLocation.responseText;if (reponseText == "NOT FOUND") document.getElementById("location_div").class = "error"; else document.getElementById("location_div").class = "success"; }
Spockrates
Thanks for your reply. Please let me know if you have any ideas why the style is not being applied.
Spockrates
There was an error in the property name in my code. I always use jquery to make these common DOM tasks.
gustavogb
A: 

In my honest opinion, jQuery is the best way to go with AJAX calls. It would also shorten your code by a lot!

Head Section

...
<script type="text/javascript" src="js/jquery.1.4.2.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
      $.get('/PP', { PAGE: 'GETLOCATIONNAME', ROUTINGNUM: location }, function(data) {
          if(data == "NOT_FOUND") {
              $('#location_div').addClass('error');
          } else {
              $('#location_div').addClass('success');
          }
      });
  });
</script>

Then you could use the same CSS as gustavogb.
Now, this may be a bit different from what you want it to do exactly, but as you can see, jQuery has significantly shortened the amount of code for an AJAX call. Also, I'm unsure of where the variable 'location' comes from, but I included in the call.

Good luck and if you need more help, you can read more at jQuery.com.

BigRossLabs
Thanks. I've used jQuery in the past for some of the cool effects--like the accordion and datepicker. How do I call your jQuery function onBlur? With JavaScript I just use this: function getLocation(location) Then this: <p class="field_top"><input type="text" id="location" name="location" size="28" maxlength="9" onblur="getLocation(this.value);" /></p>
Spockrates
You would wrap the $.get() function with another function that will load the $.get() on blur. $('input#fieldID').blur(function() { ... });
BigRossLabs