-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
92 lines (78 loc) · 2.95 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* MODALS */
// Declare variables for overlay and all open and close buttons returning an iterable Nodelist
// Using the data attribute as selector inside querySelectorAll
const overlay = document.getElementById('overlay')
const openModalButtons = document.querySelectorAll('[aria-controls]');
const closeModalButtons = document.querySelectorAll('[data-close-button]');
const modals = document.querySelectorAll('.modal');
// Add event listener for each button
// 'click' events create a variable targeting the specific modal based on data target attribute
// call the openModal function
openModalButtons.forEach(button => {
button.addEventListener('click', () => {
const specificModal = document.querySelector(button.getAttribute('aria-controls'));
openModal(specificModal);
});
});
/* code not working
const closeButton = document.querySelector('.close-button');
modals.forEach(modal => {
window.addEventListener('keydown', (event) => {
if (event.key === "Tab") {
console.log(event);
closeButton.focus();
}
});
});*/
// Add event listener to overlay to close the active modal when overlay is clicked
// Do I need the forEach since no two modals will be open at once?
overlay.addEventListener('click', () => {
const activeModal = document.querySelectorAll('.modal.active');
activeModal.forEach(activeModal => {
closeModal(activeModal);
});
});
closeModalButtons.forEach(button => {
button.addEventListener('click', () => {
const parentModal = button.closest('.modal');
closeModal (parentModal);
});
});
// Declare functions that open and close the modal
const openModal = (modal) => {
modal.classList.add('active');
overlay.classList.add('active');
};
const closeModal = (modal) => {
modal.classList.remove('active');
overlay.classList.remove('active');
};
// research aria to replace data-target
// use button element as opposed to anchor tag for semantics sake
/* COMMENTS */
// Declare form variable
const form = document.querySelector('#form');
// Listen for form submission
// Prevent default form button behavior
form.addEventListener('submit', function(e) {
e.preventDefault();
// Declare variables for the comment and name inputs
const comment = form.elements.comment;
const name = form.elements.name;
// Pass the input values to the newPost function
newPost(comment.value, name.value);
// Clear inputs after comment has been posted
comment.value = '';
name.value = '';
});
// Function for creating new posts
const newPost = (comment, name) => {
// Declare variable for ul
const posts = document.querySelector('#posts');
// Create new li element and assign it to a variable
const newComment = document.createElement('li');
// Update the text of newComment to equal the parameters passed
newComment.textContent = `${comment} - ${name}`;
// Append the new li to the ul
posts.appendChild(newComment);
}