I am working on an assignment for my JS class and I am trying to figure out how to access a specific property from an array based on user selection from a drop down menu. (I tend to try things that are ahead of what we are studying and get stuck in these binds)
First off I don't know what to actually call this data. Dataframe? Subset? Map? Array? Multi property object list? (after creating tags for this post I am wondering if this is a multidimensional-array)
I apologize if it is not described well.
So far I have been able to access the each property directly but have not figured out how to do that through the user select menu. I added ids to the value attribute for the options but don't know how to use that to access the rest of the properties after selection.
Here is my script
const flavors = [
{
"flavor":"Tangy Taro Eggy",
"description":"",
"img_src":"images/tangy-taro-eggy.jpg",
"id": 1
},
{
"flavor":"Beetle Bark Raspberry",
"description":"",
"img_src":"images/beetle-bark-raspberry.jpg",
"id": 2
},
{
"flavor":"Cali CBD-licious Creme",
"description":"",
"img_src":"images/cali-cbd-licious-creme.jpg",
"id": 3
},
{
"flavor":"Durian Delight",
"description":"",
"img_src":"images/durian-delight.jpg",
"id": 4
},
{
"flavor":"Vanilla",
"description":"",
"img_src":"vanilla.jpg",
"id": 5
}
];
var select = document.getElementById("flavor_list"); //this is the id for <select> element in my HTML
flavors.forEach(function populate_list(item){
var option = document.createElement('option');
var img_src =flavors.img_src; //This one doesn't do anything becuase the value isn't read (which I don't know what that means)
//I tried defining this variable outside of the function as well but the alert still doesnt work
option.value = item.id; // I created unique values for each option but don't know how to use them to get more info
option.innerHTML = item.flavor;
select.appendChild(option)
})
select.onchange = function fetch_info() {
alert(this.img_src) // this is just a test to see if I got the info.
//Once I figure this out I will be able to complete there rest of the assignment
}
It would be best to stick with just JS and not use Jquery or anything like that because we haven't learned any of that yet.
I appreciate any help
Kevin