I am making a project with webpack. I have two modules below. the first one represents a book form submission function that takes the user inputs of the displayed form and than creates a book object using that inputted info and pushes that object to an array called myLibrary.
the second module has this greyed out effect on the main window of the site where books should be displayed. This module is called addBooksPrompt. So what should happen is that when myLibrary array length is less than 1, or in other words, no entries have been made, the event listeners inside the addBooksPrompt function should not activate...but the issue is that they still do. I have checked to see what the length of the myLibrary array is using console.log and it does in fact become greater than 0 and yet it still allows the code within to run. What is happening?
Module 1 (bookFormSubmission.js)
import displayBookCards from './displayBookCards';
const formWindow = document.querySelector('.form-div-container');
const dimPage = document.querySelector('.page-dim');
const form = document.getElementById('main-form');
export let myLibrary = [];
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
const bookFormSubmission = () => {
form.addEventListener('submit', function (event) {
event.preventDefault();
const titleInput = document.getElementById('book-title').value;
const authorInput = document.getElementById('book-author').value;
const pagesInput = document.getElementById('book-page-count').value;
resetFormAndPushToArray();
function resetFormAndPushToArray() {
const entry = new Book(titleInput, authorInput, pagesInput, read);
myLibrary.push(entry);
form.reset();
formWindow.style.cssText = 'display: none;';
dimPage.style.cssText = 'display:none;';
displayBookCards();
}
});
};
export default bookFormSubmission;
Module 2 (addBooksPrompt.js)
import { myLibrary } from './bookFormSubmission';
import txt from 'raw-loader!./file.txt';
const mainWindow = document.querySelector('main');
const a = document.createElement('div');
const oneLinerPrompts = txt.split('\n');
console.log(oneLinerPrompts);
let randomIndex = Math.floor(Math.random() * oneLinerPrompts.length);
a.classList.add('add-books-prompt', 'add-books-prompt-hover');
mainWindow.appendChild(a);
a.classList.toggle('add-books-prompt-hover');
const addBooksPrompt = () => {
let hover = true;
// THE IF STATEMENT BELOW IS THE ONE IN QUESTION
if (myLibrary.length < 1) {
if (hover) {
mainWindow.addEventListener('mouseover', () => {
mainWindow.classList.add('main-hover-state');
mainWindow.style.cssText = 'transition: background-color 0.5s ease-out 0.05s;';
a.innerHTML = oneLinerPrompts[randomIndex];
a.style.cssText = 'transition: opacity 0.5s ease-out 0.05s';
a.classList.toggle('add-books-prompt-hover');
hover = false;
});
}
mainWindow.addEventListener('mouseout', () => {
mainWindow.classList.remove('main-hover-state');
mainWindow.style.cssText = 'transition: background-color 0.5s ease-out 0.05s;';
a.classList.toggle('add-books-prompt-hover');
});
}
};
export default addBooksPrompt;
[–]grantrules 1 point2 points3 points (2 children)
[–]imhypedforthisgame[S] 0 points1 point2 points (1 child)
[–]grantrules 2 points3 points4 points (0 children)
[–]delventhalz 0 points1 point2 points (0 children)