Мой HTML:
Код: Выделить всё
Search
Код: Выделить всё
document.addEventListener('DOMContentLoaded', function () {
const colors = [
{ color: "red", value: "#f00" },
{ color: "green", value: "#0f0" },
{ color: "blue", value: "#00f" },
{ color: "cyan", value: "#0ff" },
{ color: "magenta", value: "#f0f" },
{ color: "yellow", value: "#ff0" },
{ color: "black", value: "#000" }
];
const searchInput = document.getElementById('searchInput');
const colorList = document.getElementById('colorList');
// Function to render color cards
function renderColors(colors) {
colorList.innerHTML = ''; // Clear previous content
colors.forEach(color => {
const card = document.createElement('div');
card.classList.add('color-card');
card.innerHTML = `
${color.color}
`;
colorList.appendChild(card);
});
}
// Initial render
renderColors(colors);
// Event listener for search input
searchInput.addEventListener('input', function () {
const searchValue = this.value.toLowerCase();
const filteredColors = colors.filter(color =>
color.color.toLowerCase().includes(searchValue)
);
renderColors(filteredColors);
});
});
Код: Выделить всё
let colorDiv = document.getElementById("colorList")[0];
let searchInput = document.getElementById("searchInput")[0];
function showColor() {
colorDiv.innerHTML = `
style="color:${searchInput.value};height:100px;width:100px">
Your searched color ${searchInput.value}
`;
}
Код: Выделить всё
Document
Подробнее здесь: https://stackoverflow.com/questions/783 ... ctionality
Мобильная версия