34
loading...
This website collects cookies to deliver better user experience
handleCheck
. The first thing that this function does is it identifies the checkbox buttons of the list on the left and the detail on the right. Since there are multiple checkboxes on the list, it turns them into an iterable array. Then if the currentTarget
of the click event is the checkbox button or contains the class of "checkbox" it will call the function with the fetch request to update the JSON file. It then iterates through the array of checkboxes from the list and if the id of the checkboxes match then it change the CSS to make the button appear checked or unchecked. Each of the checkboxes have the id of the film from the API attached as the id, and iterating through the array checks the ids that are already on the DOM so the CSS can change what is already appended to the DOM. The rest of the handleCheck
function checks if the id of the list's checkbox matches the detail's checkbox, and syncs the change in CSS if they do. I also ended up adding the "contains class" if statement to all of my event handlers to clarify the intended target of the click event, so that the functionality would only be applied where its intended in a more fool-proof way. All of this turned out to look like this:function handleCheck(e) {
const detailCheck = document.querySelector("#detail-container").querySelector(".checkbox")
const cardChecks = document.querySelector("#cards-container").querySelectorAll(".checkbox")
const cardChecksArray = [...cardChecks]
const detailCheckedBtn = detailCheck.querySelector("button")
if(e.currentTarget.classList.contains("checkbox")) { // checks if click target is checkbox
updateWatched(e.currentTarget.id)
cardChecksArray.forEach(cardCheck => {
if(e.currentTarget.id === cardCheck.id) { // matches click target and list to check of correct checkbox
if(checked === true) {
e.currentTarget.querySelector("button").id = "checked"
}
else {
e.currentTarget.querySelector("button").id = ""
}
if(cardCheck.id === detailCheck.id) { // syncs check of card and detail
if(checked === true) {
cardCheck.querySelector("button").id = "checked"
detailCheckedBtn.id = "checked"
}
else {
cardCheck.querySelector("button").id = ""
detailCheckedBtn.id = ""
}
}
}
})
}
}
handleCheck
function the handleFilter
function is also a little bit complicated because quite a few things go on inside. Although instead of nested if statements like the handleCheck
function, the handleFilter
function has five else if
s, three of which have a nested if statement. Each of the else if
s act as separate functionality based on what is the desired filter. handleCheck
function. Then it acquires the id of the click target to make sure it is the filter being clicked on, and if it is it displays the dropdown menu. Then on further clicks the function checks the inner text of the clicked target and displays or hides the content based on that text. Which it also identifies the content to display or hide by iterating through all of the cards on the DOM. It also displays the targeted filter within the filter bar, so than what is currently being filtered is visible. All this is excluding “Director” since it simply a label and not a button, and has no functionality other than hovering over in order to display the dropdown menu. All of the functionality within handleFilter
are click events to show and hide items.select
and option
tags for the filter, but since I wanted a nested dropdown of directors I could not get the result I wanted. I then switched it to be a ul
, where I could not only have the functionality I wanted but also more easily customize its appearance. Since the functionality lies in the JavaScript, regardless of what tags are used the functionality remains the same, only the application of it an its appearance differ.