I have js data in a file named data.js
var data = [
{
index: "1",
flag: "Flag1",
flag_url: "https://www.w.jpg",
logo:"https://media.geeksforgeeks.org/wpcontent/uploads/20200630132503/iflag.jpg",
info: "between the...",
more_info: "With a...",
},
{
index: "2",
flag: "Flag2",
flag_url: "https://www.w.jpg",
logo:"https://media.geeksforgeeks.org/wp-content/uploads/20200630132504/uflag.jpg",
info: "S will give you...",
more_info: "lineage contains...",
},
{
my app.js has my code to change image on click of an html list
var data = data;
for (let value of data.values()) {
var logo = value.logo;
//console.log(logo)
$('li').on('click', 'a', function() {
$('#image')[0].src = logo[this.id];
});
}
my HTML is set up as such. The 'li' and 'a' tags are necessary for another function I have for a filtered search as user inputs.
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for strains..">
<ul id="myUL">
<li><a id="0"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20200630132503/iflag.jpg" width="20" height="15" > India</a></li>
<li><a id="1"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20200630132504/uflag.jpg" width="20" height="15" > USA</a></li>
<li><a id="2"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20200630132502/eflag.jpg" width="20" height="15" > England</a></li>
<li><a id="3"><img src="https://media.geeksforgeeks.org/wp-content/uploads/20200630132500/bflag.jpg" width="20" height="15" > Brazil</a></li>
</li>
</ul>
I tested the img tag with a different file that had only the logo data and it works. However, I would like to use one file throughout this project. I feel I'm close to getting it. When I console.log(logo) all the src url's are there. However, when I click the list items I receive an error: net::ERR_FILE_NOT_FOUND. I pretty confident it is the last line $('#image')[0].src = logo[this.id];. When I just have $('#image')[0].src = logo; one of the images does come through.. but that's it. I've tried:
- $('#image')[0].src = logo[this.value];
- $('#image')[0].src = logo[this.key];
- $('#image')[0].src = logo[0];
- $('#image')[0].src = logo[this.src];
I know it's going to be something simple, but I'm teaching myself JQuery as I go and obviously don't have full understanding of it, yet. Edited content as not to offend