Edit: I figured out how to pull the string effectively. My new struggle is to replace the Selectbox element name with the label in the json object produced by the submit button.
Ideally this Json object would follow the format: {[label]:[value], "Test 1:":"pass"}
I'm still very new at this, running through code academy all day but I haven't reached a point where their instructions have become useful to me yet. Thanks, for all the help guys!
js
<script type="text/javascript">
// Export to .json on click
$(document).ready(function() {
$("#btn-submit").click(function(e){
var jsonData = {};
//Answer to first question
var label = document.querySelector("label[for='usb1']").textContent;
var formData = $("#myform").serializeArray();
// console.log(formData);
$.each(formData, function() {
if (jsonData[this.name]) {
if (!jsonData[this.name].push) {
jsonData[this.name] = [jsonData[this.name]];
}
jsonData[this.name].push(this.value || '');
} else {
jsonData[this.name] = this.value || '';
}
});
console.log(jsonData);
e.preventDefault();
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
if (confirm("Save results to <SCRIPT_PATH_LOCATION>")){
download(JSON.stringify(jsonData), 'text.json', 'text/plain');
} else {
return false
}
});
});
</script>
html
<form id="myform" type="post">
<fieldset>
<div class="elements">
<label for="usb1">Test 1:</label>
<select name="usb1" id="usb1">
<option value=""></option>
<option value="pass">PASS</option>
<option value="fail">FAIL</option>
</select>
</fieldset>
</form>