0

So I have a site that has a ton of buttons, but I want to make a search bar that after every letter, it compares the contents with the id or alt of each button, so basically if I type “he”, it will show all things that have “he” in the first 2 letters of their name and not show the others

Anyone know how I would be able to do something like that?

I tried making a form that compares it when I click the submit button, but I cant seem to figure out how to make it so it updates after every letter, and I also cant figure out how to compare it properly either as it just didnt update anything: <input id="Search"></input> <button class="searchItem", id="123">123</button> <button class="searchItem", id="122">122</button> <button class="searchItem", id="213">213</button>

 const SearchBar = document.getElementById("Search");

SearchBar.addEventListener("input", updateValue);

function updateValue(e) {

const searchItems = document.getElementsByClassName("searchItem");
[searchItems].forEach(el => { 
  
  if (el.id.startsWith(e.value)) {
    el.style.display = 'block';
    print('what ok')
  } else {
    el.style.display = 'none';
    print('doesnt work')
  }
 });
};

 
5
  • 2
    Can you show us what you tried (as opposed to explaining it briefly)? In short, you may want to look at the input event, and perhaps the String#startsWith method. Commented Oct 30, 2023 at 16:37
  • Welcome to SO! I recommend all new contributors visit How to Ask for tips on how to write a question that best enables the community to provide you with assistance. It would be helpful if you included some example(s) of what you've tried so far, and included information as to why it isn't working toward your needs, including the expected behavior, the actual behavior, and any error messages you may be seeing. Good luck, and happy coding! Commented Oct 30, 2023 at 16:38
  • Could I do something like inputName.addEventListener(“input”, Event => { for all of the buttons with class name, if it starts with the input value it sets its display to block, or if not, it sets it to none}); ? Commented Oct 30, 2023 at 16:44
  • Or I could use the event type of updateValue too i think Commented Oct 30, 2023 at 16:46
  • I added the code I just made, but of course its not working either and I dont really know why Commented Oct 30, 2023 at 18:04

1 Answer 1

0

e in your event callback is the input event object, but you want instead e.target.value to get the current value of the <input>:

function updateValue(e) {
  ...
  if (el.id.startsWith(e.target.value) { 
    ... 
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry i already figured it out, but this is similar so i will mark as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.