I have a data input section in my page, in which user will have to put his/her name into a blank space and then make a selection between yes/no dropdown options. Once is filled out user clicks 'Generate" and all the information, questions and answers, appears beneath. How do I manage for the js to consider both open ended and dropdown questions/answers into the results? and How do I resize the font of the results? This is what I have so far..
<form name="myForm">
Name: <input type="text" name="firstname">
<font size="3">
<label for="q7">- Done it?</label>
</font>
<select id="q7">
<option>Yes</option>
<option>No</option>
</select>
<input type="button" onclick="populateResults()" value="Generate">
<div id="result"></div>
</form>
<script>
function populateResults() {
var selects = document.forms.myForm.querySelectorAll('select'),
result = document.getElementById("result");
Array.from(selects).forEach(function(a, i) {
var answer = a.options[a.selectedIndex].text,
question = selects[i].labels[0].textContent;
result.innerHTML += question + ' ' + answer + '
<br>';
})
}
</script>
</body>
</html>
First of all, for your option
in the select, it should have a value
property.
You are way overcomplicating the code.
You can just do this:
<form name="myForm">
Name: <input type="text" name="firstname" id="firstname">
<font size="3">
<label for="q7">- Done it?</label>
</font>
<select id="q7">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="button" onclick="populateResults()" value="Generate">
<div id="result"></div>
</form>
<script src="jquery.js"></script>
<script>
function populateResults() {
var select = $('#q7').val();
var name = $('#firstname').val();
var result = name + ' - Done it? ' + select;
$('#result').text(result);
}
</script>
This is pretty verbose but here I added some id's, removed the code from the markup, added a class to the label so I could both find an style it:
Revised markup:
<form id="myform" name="myForm">Name:
<input type="text" name="firstname" id="firstname" />
<label for="q7" class="mylabel">- Done it?</label>
<select id="q7">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<input type="button" value="Generate" id="generate" />
<div id="result"></div>
</form>
Add this CSS (removed the obsolete font tag)
.mylabel {
font-size:1.5em;
}
VERBOSE code (clean it up, verbose to make the intent obvious)
function populateResults() {
var myselect = $('#q7');
var selectedText = myselect.find('option:selected').text();
var selectedVal = myselect.val();
var question = myselect[0].labels[0].textContent;//same thing
question = myselect.siblings('.mylabel').text();// same thing
$('#result').append(question + ' :' + selectedText + ":" + selectedVal + '<br />');
}
$('#myform').on('click', '#generate', populateResults);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.