Advertisement
vasilivanov93

Untitled

Feb 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>DOM Search</title>
  6. <script src="https://code.jquery.com/jquery-3.1.0.min.js"
  7. integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
  8. crossorigin="anonymous"></script>
  9. <style>
  10. .add-controls, .search-controls {
  11. width: 20em;
  12. height: 2em;
  13. }
  14. input {
  15. position: absolute;
  16. left:6em;
  17. }
  18. .button {
  19. background-color: darkgrey;
  20. color: white;
  21. font-weight: bold;
  22. position: absolute;
  23. left: 15em;
  24. border: 1px solid black;
  25. padding: 0 5px 0 5px;
  26. }
  27. .result-controls .button {
  28. position: relative;
  29. left: 0;
  30. font-size: 50%;
  31. margin-right:1em;
  32. padding: 0;
  33. bottom: 3px;
  34. }
  35. li {
  36. list-style-type: none;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="content"></div>
  42. <script>
  43. domSearch("#content",false);
  44.  
  45. function domSearch(selector, isCaseSensitive) {
  46. $(selector).append($('<div>').addClass('add-controls').append($('<label>').text("Enter text: ").append($('<input>')))
  47. .append($('<a>').addClass('button').text('Add').on('click', addItem)));
  48.  
  49. $(selector).append($('<div>').addClass('search-controls').append($('<label>').text("Search:").append($('<input>').on('input', search))));
  50.  
  51. $(selector).append($('<div>').addClass('result-controls').append($('<ul>').addClass('items-list')));
  52.  
  53. function addItem() {
  54. let text = $('.add-controls label input').val();
  55. $('.items-list').append($('<li>').addClass('list-item').append($('<a>').addClass('button').text('X').on('click', deleteItem)).append($('<strong>').text(text)));
  56. $('.add-controls label input').val("");
  57. }
  58.  
  59. function deleteItem() {
  60. $(this).parent().remove();
  61. }
  62.  
  63. function search() {
  64. let text = $(this).val();
  65.  
  66. $('.list-item').each((index, li) => matches(li, text))
  67. }
  68.  
  69. function matches(li, text) {
  70. $(li).css('display', 'inline-block');
  71. if(isCaseSensitive) {
  72. if ($(li).find('strong').text().indexOf(text) == -1) {
  73. $(li).css('display', 'none');
  74. }
  75. } else {
  76. if ($(li).find('strong').text().toLowerCase().indexOf(text.toLowerCase()) == -1) {
  77. $(li).css('display', 'none');
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement