gammatools/web/assets/js/catalog.js

42 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

import {loadingGif} from "./loading.js";
2024-07-03 13:52:13 +00:00
document.getElementById('catalog-button').addEventListener('click', function (event) {
event.preventDefault();
// Insert the loading.gif before making the fetch request
2024-07-03 13:52:13 +00:00
let resultSection = document.getElementById('resultSection');
loadingGif(resultSection);
2024-07-03 13:52:13 +00:00
fetch(`${window.BASE_URL}/rules/creature`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
resultSection.innerHTML = ''; // Clear results
// Generate clickable elements for each creature
let table = document.createElement('table');
data.forEach(item => {
let row = document.createElement('tr');
let cell = document.createElement('td');
let btn = document.createElement('button');
btn.textContent = item;
btn.addEventListener('click', function () {
document.getElementById('creature').value = item;
document.getElementById('search-button').click();
});
cell.appendChild(btn);
row.appendChild(cell);
table.appendChild(row);
});
resultSection.appendChild(table);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
});