JavaScript AJAX Add Class to Table Cell

I have 2 select menus on my form: Type and Category. When a user selects something from the Type menu I want it to then perform a PHP script which queries a database and returns a list of matching categories for the selected Type for them to choose from in the Category menu.

This is working well but I would now like to update it so that it adds a class to the categoryGroup ("success" or "error" depending on the AJAX result). Here's my table with the 2 cells:

<div class="form-group">
  <label for="title" class="control-label col-sm-3">Type</label>
  <div class="input-group col-xs-8">
    <select class="form-control" name="type" id="type" onchange="getCategories(this.value)">
      <option value="" selected></option>
      <option value="Business">Business</option>
      <option value="Commercial">Commercial</option>
      <option value="Commercial Land">Commercial Land</option>
      <option value="Land">Land</option>
      <option value="Rental">Rental</option>
      <option value="Residential">Residential</option>
      <option value="Rural">Rural</option>
    </select>
  </div>
</div>

<div class="form-group" id="categoryGroup">
  <label for="title" class="control-label col-sm-3">Category</label>
  <div class="input-group col-xs-8" class="" id="categoryList">
    <select class="form-control" name="category" id="category">
      <option value="" selected></option>
    </select>
  </div>
</div>

and here's my script that's working to replace the Category select menu with the appropriate options from the AJAX call:

<script type="text/javascript">
  function getCategories(str) {
    if (str == "") {
      document.getElementById("categoryList").innerHTML = "";
      return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("categoryList").innerHTML = xmlhttp.responseText;
      }
    }

    xmlhttp.open("POST", "getPropertyCategories.php?type=" + str, true);
    xmlhttp.send();
  }
</script>

I can't work out how to update this to also add a class to the categoryGroup id

Answer:1

<form id="participantsForGd"> <span id="GroupID"></span> //in here group id is coming example: 2 <button id="GdStartTest">Start test</button> </form> I want to ...

<form id="participantsForGd"> <span id="GroupID"></span> //in here group id is coming example: 2 <button id="GdStartTest">Start test</button> </form> I want to ...

I want to find last descendent div of the html below. I tried but it finds the first occurrence of the div. How can I achieve this? HTML: <div class='greatparent'> <div> <div&...

I want to find last descendent div of the html below. I tried but it finds the first occurrence of the div. How can I achieve this? HTML: <div class='greatparent'> <div> <div&...

I am trying to take what a user enters in a textbox, and then write that input to a paragraph on the page, and stop the page refreshing so that they can then copy that input for use elsewhere. (it'...

I am trying to take what a user enters in a textbox, and then write that input to a paragraph on the page, and stop the page refreshing so that they can then copy that input for use elsewhere. (it'...

  1. html javascript display none
  2. html javascript display text
  3. html javascript display
  4. html javascript display all images in a folder
  5. html javascript display image
  6. html javascript display variable
  7. html javascript display date and time
  8. html javascript display block
  9. html javascript display json
  10. html javascript display running time
  11. html javascript display pdf
  12. html javascript display message
  13. html javascript display current time
  14. html javascript display current date
  15. html javascript display date
  16. html javascript display array
  17. html display javascript object
  18. html display javascript function output
  19. html display javascript variable value
  20. html display javascript return value

By using JavaScript or jQuery, I am trying to figure out if a word, say "casablanca", is available on the page? The page I am working on makes an ajax call which changes the content of the page. So I ...

By using JavaScript or jQuery, I am trying to figure out if a word, say "casablanca", is available on the page? The page I am working on makes an ajax call which changes the content of the page. So I ...

  1. searching word on web page