Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad DOM Manipulation Code #10

Closed
SamChou19815 opened this issue Dec 10, 2019 · 0 comments · Fixed by #170
Closed

Bad DOM Manipulation Code #10

SamChou19815 opened this issue Dec 10, 2019 · 0 comments · Fixed by #170
Assignees

Comments

@SamChou19815
Copy link
Contributor

SamChou19815 commented Dec 10, 2019

Example 1

if (val.length >= 3) {
a = document.createElement('DIV');
a.setAttribute('id', `${inp.id}autocomplete-list`);
a.setAttribute('class', 'autocomplete-items');
/* append the DIV element as a child of the autocomplete container: */
inp.parentNode.appendChild(a);
/* code array for results that contain course code and title array for results that contain title */
const code = [];
const title = [];
for (const attr in courses) {
if (attr.toUpperCase().includes(val)) {
code.push(courses[attr].t);
} else if (courses[attr].t && courses[attr].t.toUpperCase().includes(val)) {
title.push(courses[attr].t);
}
}
code.sort();
title.sort();
/* prioritize code matches over title matches */
const match = code.concat(title);
match.forEach(newTitle => {
/* check if the item starts with the same letters as the text field value: */
/* create a DIV element for each matching element: */
b = document.createElement('DIV');
/* make the matching letters bold: */
b.innerHTML = newTitle;
/* insert a input field that will hold the current array item's value: */
b.innerHTML += `<input type='hidden' value="${newTitle}"'>`;
/* execute a function when someone clicks on the item value (DIV element): */
b.addEventListener('click', () => {
/* insert the value for the autocomplete text field: */
inpCopy.value = b.getElementsByTagName('input')[0].value;
/* close the list of autocompleted values,
(or any other open lists of autocompleted values: */
closeAllLists();
});
a.appendChild(b);
});
}
});

Example 2

requirementString() {
if (
this.requirementsMap === null
|| this.requirementsMap.keys() === null
|| this.requirementsMap.keys().length === 0
) {
return '';
}
const keys = Array.from(this.requirementsMap.keys());
let str = 'Satisfies ';
const endStr = '</b> requirement';
const { length } = keys;
if (length === 1) {
return `${str}<b>${keys[0]}${endStr}`;
}
// loop through all but the last requirement and comma separate
for (let i = 0; i < length - 1; i += 1) {
str += `<b>${keys[i]}</b>, `;
}
// remove the comma if only 2 requirements
if (length === 2) {
str = `${str.substring(0, str.length - 2)} `;
}
return `${str}and <b>${keys[length - 1]}${endStr}`;
},

Example 3

cautionString() {
if (
this.requirementsMap === null
|| this.requirementsMap.keys() === null
|| this.requirementsMap.keys().length === 0
) {
return null;
}
let str = '';
this.requirementsMap.forEach((courses, req) => {
str += '<li>';
if (courses.length === 1) {
str += `${courses[0]} also fulfills <b>${req}</b> requirement`;
} else {
// loop through all but the last course and comma separate
for (let i = 0; i < courses.length - 1; i += 1) {
str += `${courses[i]}, `;
}
// remove the comma if only 2 requirements
if (courses.length === 2) {
str = `${str.substring(0, str.length - 2)} `;
}
str = `${str}and ${courses[courses.length - 1]} also fulfill <b>${req}</b> requirement`;
}
str += '</li>';
});
return str;
},

Consider creating a new component for these HTML and listener instead.

@willespencer willespencer mentioned this issue Mar 2, 2020
2 tasks
@willespencer willespencer linked a pull request Nov 10, 2020 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants