Skip to content Skip to sidebar Skip to footer

Toggle Sub-category Divs Based On Radio Button Selection

I used the top answer to this question to build a form that feeds into a sheet along with file upload. Now I've hit another wall. I have categories, and sub-categories. I'd like th

Solution 1:

The unexpected token is due to the function(){ line, which is invalid syntax for the jQuery document ready function. You should have:

$(function(){
    $('input[type="radio"]').click(function(){
    ...
    });
});

With that fixed, your next error will be:

Uncaught ReferenceError: $ is not defined

That's because you haven't included jQuery, which is what the $ symbol is referring to in statements like $(this). You'll want to read this for more tips about using jQuery in Google Apps Script. The short story, though: You need to add the following, adjusted for whatever version of jQuery you intend to use:

<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

Updated Form.html, which shows the appropriate <div> as you intended. It also includes the recommended doctype, html, head and body tags:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
</head>

<body>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
  <script>
    // Javascript function called by "submit" button handler,
    // to show results.
    function updateOutput(resultHtml) {
      toggle_visibility('inProgress');
      var outputDiv = document.getElementById('output');
      outputDiv.innerHTML = resultHtml;
    }

    // From blog.movalog.com/a/javascript-toggle-visibility/
    function toggle_visibility(id) {
      var e = document.getElementById(id);
      if (e.style.display == 'block')
        e.style.display = 'none';
      else
        e.style.display = 'block';
    }

    //Toggle Secondary Categories
    $(function() {
      $('input[type="radio"]').click(function() {
        if ($(this).attr("id") == "dealer") {
          $(".box").not(".dealer").hide();
          $(".dealer").show();
        }
        if ($(this).attr("id") == "online") {
          $(".box").not(".online").hide();
          $(".online").show();
        }
        if ($(this).attr("id") == "advertising") {
          $(".box").not(".advertising").hide();
          $(".advertising").show();
        }
        if ($(this).attr("id") == "pricing") {
          $(".box").not(".pricing").hide();
          $(".pricing").show();
        }
        if ($(this).attr("id") == "correspondence") {
          $(".box").not(".correspondence").hide();
          $(".correspondence").show();
        }
        if ($(this).attr("id") == "meetings") {
          $(".box").not(".meetings").hide();
          $(".meetings").show();
        }
        if ($(this).attr("id") == "other") {
          $(".box").not(".other").hide();
          $(".other").show();
        }
      });
    });
  </script>

  <div id="formDiv">
    <!-- Form div will be hidden after form submission -->
    <form id="myForm">

      Name:
      <input name="name" type="text" /><br/>
      Co-Op Amount: <input name="amount" type="text" /><br/>
      Co-Op Split:<br />
      <input type="radio" name="split" value="100%">100&#37;<br>
      <input type="radio" name="split" value="50/50">50/50<br>
      <input type="radio" name="split" value="75/25">75/25<br> Other: <input type="text" name="split" /><br /> Reason for Co-Op: <input name="reason" type="text" cols="20" rows="5" /><br />
      Brand:
      <select name="brand">
        <option>Select Option</option>
        <option>Bluebird</option>
        <option>Brown</option>
        <option>Ferris</option>
        <option>Giant Vac</option>
        <option>Honda</option>
        <option>Hurricane</option>
        <option>Little Wonder</option>
        <option>RedMax</option>
        <option>SCAG</option>
        <option>Snapper Pro</option>
        <option>Sno-Way</option>
        <option>SnowEx</option>
        <option>Wright</option>
        <option>Ybravo</option>
      </select><br/>
      Co-Op Category:<br />
      <input type="radio" name="category" id="dealer" value="Dealer Advertising">Dealer Advertising<br />
      <input type="radio" name="category" id="online" value="Digital/Online Marketing">Digital/Online Advertising<br />
      <input type="radio" name="category" id="meetings" value="Meetings and Schools">Meetings and Schools<br />
      <input type="radio" name="category" id="advertising" value="PACE Advertising">PACE Advertising<br />
      <input type="radio" name="category" id="pricing" value="Program Pricing Promotions">Program Pricing Promotions<br />
      <input type="radio" name="category" id="correspondence" value="PACE-to-Dealer Correspondence">PACE-to-Dealer Correspondence<br />
      Other: <input type="text" id="other" name="category" /><br />

      <div class="dealer box" style="display: none;">DEALER</div>
      <div class="online box" style="display: none;">ONLINE</div>
      <div class="meetings box" style="display: none;">MEETINGS</div>
      <div class="advertising box" style="display: none;">ADVERTISING</div>
      <div class="pricing box" style="display: none;">PRICING</div>
      <div class="correspondence box" style="display: none;">CORRESPONDENCE</div>


      Email: <input name="email" type="text" /><br/>
      Message: <textarea name="message" style="margin: 2px; height: 148px; width: 354px;"></textarea><br/>
      School Schedule (Image Files Only): <input name="myFile" type="file" /><br/>
      <input type="button" value="Submit" onclick="toggle_visibility('formDiv'); toggle_visibility('inProgress');
        google.script.run
          .withSuccessHandler(updateOutput)
          .processForm(this.parentNode)" />
    </form>
  </div>

  <div id="inProgress" style="display: none;">
    <!-- Progress starts hidden, but will be shown after form submission. -->
    Uploading. Please wait...
  </div>

  <div id="output">
    <!-- Blank div will be filled with "Thanks.html" after form submission. -->
  </div>

</body>

</html>

Post a Comment for "Toggle Sub-category Divs Based On Radio Button Selection"