I am attempting to filter a JSON array based on a value from a URL and extract the vernacularName
attribute. While debugging in F12, I can successfully retrieve the correct variable using record[0].vernacularName;
, however, the code below still does not work.
document.getElementById("demo").innerHTML = datasetID;
functions as expected, but document.getElementById("demo").innerHTML = vernacularName;
does not.
My end goal is to store the vernacularName as a session variable.
<body>
<div class="">
<h3>testing data</h3>
<p id="demo" class = 'pl-5'></p>
</div>
<script type="text/javascript">
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const species = urlParams.get('species')
const bone = urlParams.get('bone')
const datasetID = urlParams.get('datasetID')
var data;
$.getJSON("json/data.json", function(json){
data = json;
});
record = data.filter(r => r.datasetID == datasetID);
var vernacularName = record[0].vernacularName;
// const vernacularName2 = record.vernacularName;
$_SESSION["v"] = vernacularName;
</script>
<script>
document.getElementById("demo").innerHTML = vernacularName;
</script>