Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text area and checkboxes to display with radio buttons

As displayed below I am working on a survey, I've spent most of the night getting the radio buttons to work and I have now ran into a new issue. How do I go about getting the text fields and checkboxes to display in addition to the radio buttons when the user clicks submit.

Currently, only question 2 responses are recorded and displayed. Thank You

http://jsbin.com/zuxoqe/1/edit?html,output

HTML

<div id="container">

<div id="main" class="cf">
        <div id="content">
            <div id="text">

<form action="#" method="post" class="mySurvey" id="mySurveyID">
    <fieldset>

        <p>Personal Information:
        <br>
        First name: <label><input type="text" name="firstname" value=""></label>
        <br>
        Last name: <label><input type="text" name="lastname" value=""></label>
        <br>
        Gender:
        <select name="gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
        </select></p>

    <p>Question 1: How did you hear about us? Check ALL that apply</p>
    <p>
        <label><input type="checkbox" name="Q3" value="internet">Internet<label>
        <label><input type="checkbox" name="Q3" value="tv">TV<label>
        <label><input type="checkbox" name="Q3" value="radio">Radio<label>
        <label><input type="checkbox" name="Q3" value="newspaper">Newspaper<label>
        <label><input type="checkbox" name="Q3" value="mouth">Word of Mouth<label>
    </p>

    <p>Question 2: Overall HOW would you rate your Sales Representative?</p>
    <p>
        <label><input type="radio" name="ques5" value="Amazing"> Amazing</label>
        <label><input type="radio" name="ques5" value="Very Good"> Very Good</label>
        <label><input type="radio" name="ques5" value="Neutral"> Neutral</label>
        <label><input type="radio" name="ques5" value="Fair"> Fair</label>
        <label><input type="radio" name="ques5" value="Poor"> Poor</label>
    </p>

    <p>Question 3: What factors contributed to your purchase of this product?
    <p> 
        <label><textarea name="Q2" rows="5" cols="60"></textarea><label>
    </p>

    <p><button type="button" name="getSelection">Submit</button></p>
    </fieldset>
</form>

            </div> <!-- end text -->
        </div> <!-- end content -->

</div> <!-- end container div -->         

</body>
</html>

JS

<script type="text/javascript">
// to remove from global namespace
(function() {

    function getRadioSelection(form, name) {
        var val;
        var radios = form.elements[name];

        for (var i=0, len=radios.length; i<len; i++) {
            if ( radios[i].checked ) {
                val = radios[i].value;
                break;
            }
        }
        return val;
    }


    document.forms['mySurveyID'].elements['getSelection'].onclick = function() {
        alert( 'The selected radio button\'s value is: ' + getRadioSelection(this.form, 'ques5') );
    };


    // disable submission of all forms on this page
    for (var i=0, len=document.forms.length; i<len; i++) {
        document.forms[i].onsubmit = function() { return false; };
    }

}());
</script>
like image 783
Justin R Avatar asked Oct 19 '15 12:10

Justin R


1 Answers

You do not need to use a custom loop to retrieve the selected combos or checkboxes. If I may have a suggestion, you may use querySelector and querySelectorAll that are designed for this.

Also you may want to use form.addEventListener("submit", callback); instead of form.onsubmit so that you can add more than one listener for the submission of a form and that you do not have to hack into your button's onclick event.

Here is a working example:

// Avoid polluting global namespace.
(function() {
    // Helpers
    function forEach(list, callback){ return Array.prototype.forEach.call(list, callback); };
    function map(list, callback){ return Array.prototype.map.call(list, callback); };
  
    var survey = document.forms['mySurveyID'];
    // Listen for submit events (need a "submit" input instead of
    // "button", cf HTML).
    survey.addEventListener("submit", function(eat){
      // Retrieve question 1's checked inputs.
      var q1CheckedInputs = survey.querySelectorAll("input[name=Q3]:checked");
      // Extract inputs' values.
      var q1Answers = map(q1CheckedInputs, function(inputNode){
        return inputNode.value;
      });

      // Retrieve question 2's answer.
      var q2Answer = survey.querySelector("input[name=ques5]:checked").value;

      // Retrieve question 3's answer
      var q3Answer = survey.querySelector("textarea[name=Q2]").value;

      // Show results.
      alert('Q1: "' + q1Answers.join('", "') + '\n' +
            'Q2: "' + q2Answer + '\n' +
            'Q3: "' + q3Answer + '"');
    });
    
    // Disable submission of all forms on this page.
    forEach(document.forms, function(form){
      form.addEventListener("submit", function(evt){
        evt.preventDefault();
      });
    });
    
}());
<!DOCTYPE html>
<body>

<div id="container">
    
<div id="main" class="cf">
        <div id="content">
            <div id="text">

<form action="#" method="post" class="mySurvey" id="mySurveyID">
    <fieldset>

		<p>Personal Information:
        <br>
        First name: <label><input type="text" name="firstname" value=""></label>
        <br>
		Last name: <label><input type="text" name="lastname" value=""></label>
        <br>
        Gender:
        <select name="gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
        </select></p>
		
	<p>Question 1: How did you hear about us? Check ALL that apply</p>
	<p>
		<label><input type="checkbox" name="Q3" value="internet">Internet<label>
		<label><input type="checkbox" name="Q3" value="tv">TV<label>
		<label><input type="checkbox" name="Q3" value="radio">Radio<label>
		<label><input type="checkbox" name="Q3" value="newspaper">Newspaper<label>
		<label><input type="checkbox" name="Q3" value="mouth">Word of Mouth<label>
	</p>
	
    <p>Question 2: Overall HOW would you rate your Sales Representative?</p>
    <p>
        <label><input type="radio" name="ques5" value="Amazing"> Amazing</label>
        <label><input type="radio" name="ques5" value="Very Good"> Very Good</label>
        <label><input type="radio" name="ques5" value="Neutral"> Neutral</label>
        <label><input type="radio" name="ques5" value="Fair"> Fair</label>
		<label><input type="radio" name="ques5" value="Poor"> Poor</label>
    </p>
	
	<p>Question 3: What factors contributed to your purchase of this product?
	<p>	
		<label><textarea name="Q2" rows="5" cols="60"></textarea><label>
    </p>
	
    <p><button type="submit" name="getSelection">Submit</button></p>
    </fieldset>
</form>

            </div> <!-- end text -->
        </div> <!-- end content -->

</div> <!-- end container div -->         


</body>
</html>
like image 65
Quentin Roy Avatar answered Sep 26 '22 15:09

Quentin Roy