Answer the question
In order to leave comments, you need to log in
How to do site search js?
Hello everyone, I wanted to try to search the site, but it doesn’t work...
Here is the code
(content array with information)
var CATALOG = [
{
name: "Harry Potter",
house: "House: Gryffindor",
img: "http://hp-api.herokuapp.com/images/harry.jpg",
},
{
name: "Hermione Granger",
house: "House: Gryffindor",
img: "http://hp-api.herokuapp.com/images/hermione.jpeg",
},
{
name: "Ron Weasley",
house: "House: Gryffindor",
img: "http://hp-api.herokuapp.com/images/ron.jpg",
},
{
name: "Draco Malfoy",
house: "House: Slytherin",
img: "http://hp-api.herokuapp.com/images/draco.jpg",
},
{
name: "Cho Chang",
house: "House: Ravenclaw",
img: "http://hp-api.herokuapp.com/images/cho.jpg",
},
{
name: "Cedric Diggory",
house: "House: Hufflepuff",
img: "http://hp-api.herokuapp.com/images/cedric.png",
},
]
var htmlCatalog = ""
CATALOG.forEach(({name, house, img}) => {
htmlCatalog += `
<div id="box" class="container">
<div class="text">
<h3>${name}</h3>
<h4>${house}</h4>
</div>
<img src="${img}">
</div>
`
var html = `
<div class="wrapper">
<div class="block">
<div class="center">
<span>search</span>
<p>search and do not lose</p>
</div>
<div class="search1">
<div class="search2">
<input placeholder="search" class="search" type="search" id="search">
<div class="btn" id="btn"></div>
</div>
</div>
<div class="contant">
${htmlCatalog}
</div>
</div>
</div>
`
contants.innerHTML = html
})
CATALOG.forEach(({name}) => {
btn.addEventListener("click", () => {
if(search.value == {name}) {
console.log("Нашел")
}
else {
console.log("Не нашел")
}
})
})
Answer the question
In order to leave comments, you need to log in
search.value == {name}
why did you turn the name string into the {name} object? I suspect that you do not fully understand destructuring and you are confused by the entry CATALOG.forEach(({name}).
If you are doing a simple comparison, then compare the strings
search.value == name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question