Search code examples
javascriptjqueryhtmlgoogle-apps-scriptgoogle-apps

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 the sub-categories to only show up IF their parent category has been selected. I just can't figure out A) where I need to put the code (on our website it's right in with the HTML), I've tried putting it in the HTML file and the Code.gs file, or B) if the code I'm using is even right.

Here's the form - the "Co-Op Category" is the parent categories, I have hidden divs for each category that would hold the 'child categories'

HTML:

   <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';
  }
</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>

Code.gs:

var submissionSSKey = '1zzRQwgXb0EN-gkCtpMHvMTGyhqrx1idXFXmvhj4MLsk';
var folderId = "0B2bXWWj3Z_tzTnNOSFRuVFk2bnc";

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Form.html');
  template.action = ScriptApp.getService().getUrl();
  return template.evaluate();
}


function processForm(theForm) {
  var fileBlob = theForm.myFile;
  var folder = DriveApp.getFolderById(folderId);
  var doc = folder.createFile(fileBlob);

  // Fill in response template
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  var name = template.name = theForm.name;
  var amount = template.amount = theForm.amount;
  var split = template.split = theForm.split;
  var reason = template.reason = theForm.split;
  var brand = template.brand = theForm.brand;
  var category = template.category = theForm.category;
  var message = template.message = theForm.message;
  var email = template.email = theForm.email; 
  var fileUrl = template.fileUrl = doc.getUrl();

  // Record submission in spreadsheet
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheets()[0];
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 9).setValues([[name,amount,split,reason,category,brand,message,email,fileUrl]]);

  // Return HTML text for display in page.
  return template.evaluate().getContent();
}

//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();
        }
    });
};

This bit specifically is where I'm having trouble:

//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();
            }
        });
    };

Solution

  • 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>