I am having difficulty with a Javascript loop syntax. I have a form and when the user select an date from an select input a onchange function is triggered. I need to loop though the array find the key:value pair that matches what was selected and populate the next input element with the value from the array. I have trouble wrapping my mind around Javascript.
Here is my array which is saved in a text file 'meetinfo_array.txt'.
So if the use selected the date "2019-04-18", then in the next input textfield id="ho_acronym" should be populated with the value "abc".
Here script section. The loop always returns "Not Found".
Here is the chunk of text from the form HTML
Thanks for any help
Here is my array which is saved in a text file 'meetinfo_array.txt'.
Code:
[{"meetdate":"2019-04-18","topic":"TBA, "handout":"abc"},
{"meetdate":"2019-05-02","topic":"TBA","handout":"xyz"}]
So if the use selected the date "2019-04-18", then in the next input textfield id="ho_acronym" should be populated with the value "abc".
Here script section. The loop always returns "Not Found".
JavaScript:
<script>
function getAcronym() {
var x = document.getElementById('meetingdate');
var mdate = x.value;
var file = "meetinfo_array.txt";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var arr = JSON.parse(rawFile.responseText);
}
for(var i=0; i<arr.length; i++) {
if (arr['meetdate'] == mdate) {
document.getElementById("ho_acronym").value = ho;
}
else {
//for testing
alert("Not found");
{break};
}
}
}
}
rawFile.send(null);
}
</script>
Here is the chunk of text from the form HTML
Code:
<select class="quform-tooltip" id="meetingdate" name="meetingdate" title="Meeting Date" onchange="getAcronym()">
<?php
//Retrieve the data from our text file.
$fileContents = file_get_contents('meetinfo_array.txt');
//Convert the JSON string back into an array.
$info_decoded = json_decode($fileContents, true);
// more code to make list for option values ...
?>
<option value="<?php echo $item;?>"><?php echo date("F d, Y", strtotime($item)); ?></option> <?php } ?>
<script>
function getAcronym() {
var x = document.getElementById('meetingdate');
var mdate = x.value;
var file = "meetinfo_array.txt";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var arr = JSON.parse(rawFile.responseText);
}
for(var i=0; i<arr.length; i++) {
if (arr['meetdate'] == mdate) {
document.getElementById("ho_acronym").value = ho;
}
else {
//for testing
alert("Not found");
{break};
}
}
}
}
rawFile.send(null);
}
</script>
<input type="lctext" name="ho_acronym" id="ho_acronym" />
Thanks for any help