Stack Overflow Asked by Umutambyi Gad on November 7, 2021
I want to add particular course only once in the following container and I tried to use this condition course.value !== added.textContent
to avoid duplicates but it does nothing as I still see the duplicated courses.
For example, if you add html
and add it again the condition acts like it is not available and I want to solve this.
the following are my code:
course.focus();
add.onclick = () => {
let added = document.createElement('div');
added.className = 'added';
if ((course.value).trim()) {
/*statement to avoid duplicate*/
if (course.value !== added.textContent) {
added.textContent = (course.value).replace(' ', '-');
added.title = course.value + ' course';
paste.appendChild(added);
course.value = '';
}
}
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #ff5;
}
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
width: 50%;
height: 50%;
}
#paste {
width: 100%;
height: 100%;
border: 1px solid darkred;
display: flex;
flex-flow: row wrap;
flex-basis: 1;
}
#copy {
text-align: center;
}
input {
width: 95%;
font-weight: 900;
height: 30px;
outline: none;
border: none;
background: none;
border-bottom: 2px solid green;
border-radius: 5px;
font-size: 18px;
text-align: center;
}
button {
width: 140px;
height: 25px;
border-radius: 10px;
outline: none;
border: none;
font-size: 16px;
margin-top: 15px;
background: green;
color: #fff;
font-weight: 900;
}
.added {
position: relative;
margin: 3px;
width: auto;
padding: 5px;
background: grey;
border-radius: 5px;
text-align: center;
align-self: baseline;
}
<div class="container">
<div id="copy">
<input type="text" name="course" id="course" placeholder="Enter course">
<button id="add">add</button>
</div>
<div id="paste"></div>
</div>
Store all the added courses in a Set
and check if the entered course already exists before appending it to the container.
course.focus();
const courses = new Set;
add.onclick = () => {
let added = document.createElement('div');
added.className = 'added';
const val = course.value.trim();
if (val && !courses.has(val)){
courses.add(val);
added.textContent = (course.value).replace(' ', '-');
added.title = course.value + ' course';
paste.appendChild(added);
course.value = '';
}
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #ff5;
}
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
width: 50%;
height: 50%;
}
#paste {
width: 100%;
height: 100%;
border: 1px solid darkred;
display: flex;
flex-flow: row wrap;
flex-basis: 1;
}
#copy {
text-align: center;
}
input {
width: 95%;
font-weight: 900;
height: 30px;
outline: none;
border: none;
background: none;
border-bottom: 2px solid green;
border-radius: 5px;
font-size: 18px;
text-align: center;
}
button {
width: 140px;
height: 25px;
border-radius: 10px;
outline: none;
border: none;
font-size: 16px;
margin-top: 15px;
background: green;
color: #fff;
font-weight: 900;
}
.added {
position: relative;
margin: 3px;
width: auto;
padding: 5px;
background: grey;
border-radius: 5px;
text-align: center;
align-self: baseline;
}
<div class="container">
<div id="copy">
<input type="text" name="course" id="course" placeholder="Enter course">
<button id="add">add</button>
</div>
<div id="paste"></div>
</div>
Answered by Unmitigated on November 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP