1

May be there is some silly mistake I made. It's been 2 days I can't figure out the error. Here is what I want to achieve: When I type something on my search box the blocks which matches with my search there display will be block and others display will be none. Here is my HTML below.

<form class="d-flex">
  <input id="searchInput" class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success" type="submit">Search</button>
</form>

Here is the JavaScript Code. The problem is actually here. From the includes part it's not working properly.:

const search = document.getElementById('searchInput');
search.addEventListener('input', function(){
    const inputValue = search.value.toLowerCase();
    const noteCard = document.getElementsByClassName('cards');
    Array.from(noteCard).forEach(function(element){
        const cardTxt = element.getElementsByTagName('p')[0].innerText;
        console.log(typeof cardTxt)
        if(cardTxt.includes(inputValue)){
            console.log("yes included");
            element.style.display = 'block';
        }
        else{
            console.log('not included')
            element.style.display = 'none';

        }
    })
})

You may think what is cards, please have a look:

let html= "";
    noteArray.forEach(function(element, index){
        html +=`
        <div class="card my-2 mx-2 cards" style="width: 18rem;">
        <div class="card-body">
          <h5 class="card-title">Note ${index + 1}</h5>
          <p class="card-text">${element}</p>
          <button id=${index} onclick="deleteNote(this.id)" class="btn btn-primary">Delete Note</button>
        </div></div>
        `;

I can't find a error. If you can figure it out. Please let me know. You may need to see the full code. Here is my codepen link: https://codepen.io/techsinkv1/project/editor/Zvdvrg

async await
  • 1,967
  • 1
  • 8
  • 19
Tech Sink
  • 52
  • 7
  • 1
    Please be more specific than "Not working properly". What isn't happening that should? What is happening that shouldn't? What console errors are there, if any? – Jon P Dec 17 '21 at 05:38
  • Okay, I edited now. – Tech Sink Dec 17 '21 at 05:49
  • 1
    @TechSink Search seems to be working. What isn't working? – Ramesh Reddy Dec 17 '21 at 05:54
  • from the includes part it's not working. – Tech Sink Dec 17 '21 at 14:41
  • this is because you put your include in the `HTML > HEAD` part – Mister Jojo Dec 21 '21 at 01:46
  • @MisterJojo Sorry, I don't get it. Can you please elaborate what you said or write the correct code? Thank you. – Tech Sink Dec 21 '21 at 17:03
  • https://stackoverflow.com/questions/9916747/why-is-document-body-null-in-my-javascript/33722566#33722566 – Mister Jojo Dec 21 '21 at 17:49
  • @MisterJojo That's not the problem. My script is already into event listener and it's not running before the html body loaded. – Tech Sink Dec 21 '21 at 18:17
  • Ok, I didn't guess! anyway given the little information you give, I don't think we can know what you did ... – Mister Jojo Dec 21 '21 at 18:28
  • @MisterJojo Please have a look carefully to my question. I added my whole code link end of my question. – Tech Sink Dec 21 '21 at 18:39
  • CodePan doesn't work on my connection. And SO offer codding snippet feature you can use instead – Mister Jojo Dec 21 '21 at 18:42
  • Why did you convert your search text completely to lowercase `inputValue = search.value.toLowerCase();` ? apart from case sensitive stuff your code seems to be working fine – Anil Parshi Dec 22 '21 at 06:58
  • @groovy_guy there is no such reason to do that. It's just normally I did it. My code it seems ok to me. But, still it's not working. I don't know why? Can you please check it on your device and fix it for me? I would be glad if you do that. – Tech Sink Dec 22 '21 at 07:13
  • @TechSink I have reviewed the code pen linked and it appears to be functioning as intended. The only thing that does not seem to serve a purpose is your search button. You have the search set to update on every input, not on the button press. What intended functionality were you missing, or are you just looking for feedback on your project? – async await Dec 22 '21 at 17:21

0 Answers0