This project is a small mobile site. I wanted to serve the HTML for the bibliography dynamically through JS. Here's the code that works perfectly in Firefox:
let butlerBooks = [];
fetch("butler.json").then(res => {
return res.json();
}).then(loadedButlerBooks => {
console.log(loadedButlerBooks);
butlerBooks = loadedButlerBooks;
loadBooks();
}).catch(err => {
console.error(err);
});
const biblioItem = document.getElementById('biblioCards');
function loadBooks() {
butlerBooks.forEach(biblioCard => {
const biblioContent = `<div id="biblioBorder" class="biblio-card-container">
<div id="biblioTitle" class="biblio-list-item">
<div class="biblio-date">${biblioCard.year}</div>
<div class="biblio-booktitle">${biblioCard.title}</div>
</div>
<div id="biblioExpand" class="biblio-card-expand"><img src="./img/expand.png" alt="expand button"></div>
<div class="biblio-expanded">
<div class="biblio-expanded-top">
<div class="biblio-subtitle">${biblioCard.subtitle}</div>
<div id="biblioMinimize" class="biblio-minimize"><img src="./img/minimize.png" alt="minimize button"></div>
</div>
<div id="biblioSubcard" class="biblio-subcard">
<p class="biblio-booksummary">${biblioCard.summary}</p>
<a class="biblio-btn-link" href="${biblioCard.url}" target="_blank"><button id="biblioBtn" class="biblio-btn btn" type="button" name="button">View on Goodreads</button>
</div>
</div>
</div>`
biblioItem.insertAdjacentHTML('afterbegin', biblioContent);
if (biblioCard.series === "Patternist") {
console.log('I\'m a Patternist Book!');
biblioBorder.style.border = "0.2em solid #434463";
biblioBorder.style.borderLeft = "12px solid #434463";
biblioBtn.style.backgroundColor = "#434463";
biblioSubcard.style.background = "rgba(67, 68, 99, 0.3)";
} else {
biblioBorder.style.border = "0.2em solid #5EA878";
biblioBorder.style.borderLeft = "12px solid #5EA878";
biblioBtn.style.backgroundColor = "#5EA878";
biblioSubcard.style.background = "rgba(94, 168, 120, 0.3)";
}
This is what the code looks like in Firefox: Bibliography Desired View This is what it looks like in other browsers: Other Browser's Broken view
I get no error messages in Firefox, but the error in Chrome for example is as follows:
TypeError: Cannot set property 'border' of undefined
at main.js:48
at Array.forEach (<anonymous>)
at loadBooks (main.js:25)
at main.js:18
(anonymous) @ main.js:20
Promise.catch (async)
(anonymous) @ main.js:19
I'm not sure how to fix this. Is it a fetch issue? An id issue? A forEach loop issue? What exactly is preventing the border property from implementing in other browsers? All of the JS I'm using should be supported in most modern browsers (ES6, Arrow Functions, InsertAdjacentHTML, etc) so it's a headscratcher for me.
Thank you in advance for any help!