Search code examples
javascriptarrayslikert

Show text box for comments if selecting anything lower than 3 on a likert 1-5 scale


I am trying to create a survey with a 1-5 scale option. I want to have javascript show a textbox for comments if a user selection a value of 1-3. Could anyone help me with the javascript code to do so?

Here is my code:

<li>
  <span class="survey-question">How would you rate your experience with online shopping?</span>
  <div class="survey-answer">
    <label>
          <input class="radio-button" type="radio" name="required" value="1"> Horrible
        </label>
    <label>
        <input class="radio-button" type="radio" name="required" value="2"> Bad
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="3"> So-So
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="4"> Great
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="5" checked> Awesome!
        </label>
  </div>


  <!-- If an answer <= "So-So or less", display: -->
  <div id="improvement" class="new">
    <span class="survey-question">How can we improve?</span>
    <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
  </div>
</li>


Solution

  • Just need to wire up a change listener, then show hide based on your condition (I included JQuery just for ease, but you could use vanilla, no problem):

    Based on your comments, I've updated the code snippet to handle multiple questions. I had to remove the "id" field so you didn't have duplicate identifiers, and I targeted the textbox via the ".new" class - while that works for the example, it's a bit rigid using the .parent().parent() - but it's enough to get you on your way. Also, you have to give the "name" field of the radio buttons a different identifier per group.

    $(".new").hide();
    $("input").on("change",handleChange);
    function handleChange(e){
        var rating = $(e.currentTarget).val();
        var $_textBox = $(e.currentTarget).parent().parent().next(".new");
        if(rating < 4 && $_textBox){
            $_textBox.show();
        }else{
            $_textBox.hide();
        }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <li>
      <span class="survey-question">QUESTION 1: How would you rate your experience with online shopping?</span>
      <div class="survey-answer">
        <label>
              <input class="radio-button" type="radio" name="required" value="1"> Horrible
            </label>
        <label>
            <input class="radio-button" type="radio" name="required" value="2"> Bad
            </label>
        <label>
              <input class="radio-button" type="radio" name="required" value="3"> So-So
            </label>
        <label>
              <input class="radio-button" type="radio" name="required" value="4"> Great
            </label>
        <label>
              <input class="radio-button" type="radio" name="required" value="5" checked> Awesome!
            </label>
      </div>
    
    
      <!-- If an answer <= "So-So or less", display: -->
      <div class="new">
        <span class="survey-question">How can we improve?</span>
        <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
      </div>
    </li>
    <br>
    <li>
      <span class="survey-question">QUESTION 2: How would you rate your experience with online shopping?</span>
      <div class="survey-answer">
        <label>
              <input class="radio-button" type="radio" name="required2" value="1"> Horrible
            </label>
        <label>
            <input class="radio-button" type="radio" name="required2" value="2"> Bad
            </label>
        <label>
              <input class="radio-button" type="radio" name="required2" value="3"> So-So
            </label>
        <label>
              <input class="radio-button" type="radio" name="required2" value="4"> Great
            </label>
        <label>
              <input class="radio-button" type="radio" name="required2" value="5" checked> Awesome!
            </label>
      </div>
    
    
      <!-- If an answer <= "So-So or less", display: -->
      <div class="new">
        <span class="survey-question">How can we improve?</span>
        <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
      </div>
    </li>