Answer the question
In order to leave comments, you need to log in
How to create a substitution with error tolerance?
Hello, I'm sawing my forum, on the page for creating new messages there is an input where a person enters tags. The task is to make auto-completion below, depending on what the person entered, as here, on habr qna. I implemented like this:
//tag - инпут, куда человек вводит теги. result - div, куда выводятся варианты тегов, на странице находится под инпутом
//result-elem - предлагаемый тег внутри result
tag.addEventListener('input', ()=>{ //при любом изменении инпута тегов благодаря этому событию происходит триггер
if(tag.value.length > 2) { //при вводе больше 2 букв начинается подбор
result.style.opacity = '1'; //показываем div результатов
while (result.firstChild) { //очищаем из него предыдущие значения
result.removeChild(result.firstChild);
}
tags.forEach((elem)=>{ //tags - массив тегов вида ['apple','orange','pear'], перебираем методом forEach
let string = elem; //string принимает значения тега
let flag = string.includes(tag.value); //если строка string содержит содержимое инпута, то во flag кладется true
if (flag) {
insertElem(elem); //функция, ничего особенного, создает и вставляет элемент p в result с содержимым elem
}
});
} else {
result.style.opacity = '0'; // если введено меньше 2 символов, убираем result
}
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question