Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. searchBox = document.querySelector("#search-box");
  2. contracts = document.querySelector(".examples");
  3. var when = "keyup";
  4.  
  5. searchBox.addEventListener("keyup", function (e)
  6. {
  7. //declare variables
  8. var text = e.target.value;
  9. var options = contracts.querySelectorAll('.item>a');
  10.  
  11. //loop through all examples to find a match
  12. for(var i = 0; i < options.length; i++)
  13. {
  14. var option = options[i];
  15. var optionText = option.text;
  16. var lowerOptionText = optionText.toLowerCase();
  17. var lowerText = text.toLowerCase();
  18. var regex = new RegExp("^" + text, "i");
  19. var match = optionText.match(regex);
  20. var contains = lowerOptionText.indexOf(lowerText) != -1;
  21.  
  22. //hide all content that doesn't match
  23. if(match || contains && text !== '')
  24. {
  25. option.style.display = 'block';
  26. }
  27. else
  28. {
  29. option.style.display = 'none';
  30. }
  31. }
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement