I have an html project to which I need to add 4 buttons filled from a JSON file using an array.
The parameters are taken depending on the link id.
<div class="d-flex justify-content-around mt-4">
<a href="#" id="button-1" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-1"> Button 1
</a>
<a href="#" id="button-2" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-2"> Button 2
</a>
<a href="#" id="button-3" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-3"> Button 3
</a>
<a href="#" id="button-4" class="btn btn-primary">
<img src="" alt="Icon" id="icon-button-4"> Button 4
</a>
</div>
</div>
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const repoId = urlParams.get('id');
$.getJSON('repositories.json', function (data) {
const repo = data.find(r => r.id === repoId);
if (repo.buttons && Array.isArray(repo.buttons)) {
repo.buttons.forEach((button, index) => {
const btn = $(`#button-${index + 1}`);
if (btn.length > 0) {
btn.attr('href', button.link || '#');
btn.find('img').attr('src', button.icon || 'default-icon.png');
btn.find('span').text(button.text || `Button ${index + 1}`);
}
});
}
});
</script>
</body>
</html>
Code from JSON file:
[
{
"id": "repo1",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
},
{
"id": "repo2",
"buttons": [
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl1"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl2"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl3"
},
{
"link": "exmpl.html",
"icon": "exmpl.png",
"text": "btn exmpl4"
}
]
}
]
I tried renaming repo.buttons to repoId.buttons, but it didn’t help (fixed the error in the console, but the problem remained)
Console error: Uncaught ReferenceError: repo is not defined


