Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>DOM Search</title>
- <script src="https://code.jquery.com/jquery-3.1.0.min.js"
- integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
- crossorigin="anonymous"></script>
- <style>
- .add-controls, .search-controls {
- width: 20em;
- height: 2em;
- }
- input {
- position: absolute;
- left:6em;
- }
- .button {
- background-color: darkgrey;
- color: white;
- font-weight: bold;
- position: absolute;
- left: 15em;
- border: 1px solid black;
- padding: 0 5px 0 5px;
- }
- .result-controls .button {
- position: relative;
- left: 0;
- font-size: 50%;
- margin-right:1em;
- padding: 0;
- bottom: 3px;
- }
- li {
- list-style-type: none;
- }
- </style>
- </head>
- <body>
- <div id="content"></div>
- <!--<script src="p05_DOM%20Search.js"></script>-->
- <!--<script src="p05_test.js"></script>-->
- <script>
- domSearch("#content",false);
- function domSearch(selector, isCaseSensitive) {
- let contentDiv = $(selector);
- let divAdd = $('<div class="add-controls">');
- let labelAdd = $('<label>Enter text:</label>');
- let inputAdd = $('<input>');
- let anchorAdd = $('<a class="button" style="display: inline-block">Add</a>');
- inputAdd.appendTo(labelAdd);
- labelAdd.appendTo(divAdd);
- anchorAdd.appendTo(divAdd);
- divAdd.appendTo(contentDiv);
- let divSearch = $('<div class="search-controls">');
- let labelSearch = $('<label>Search:</label>');
- let inputSearch= $('<input>');
- inputSearch.appendTo(labelSearch);
- labelSearch.appendTo(divSearch);
- divSearch.appendTo(contentDiv);
- let divResult = $('<div class="result-controls">');
- let ulResult = $('<ul class="items-list"></ul>');
- ulResult.appendTo(divResult);
- divResult.appendTo(contentDiv);
- let ul = $('.items-list');
- let addBtn = $('.add-controls a');
- let addText = $('.add-controls input');
- let searchInput = $('.search-controls input');
- addBtn.on('click', addElements);
- searchInput.on('input', searchElements);
- function addElements(event) {
- let li = $('<li class="list-item"></li>');
- let anchor = $('<a class="button">X</a>').on('click', deleteElements);
- let elementValue = $(addText).val();
- let element = $('<strong>').text(elementValue);
- anchor.appendTo(li);
- element.appendTo(li);
- li.appendTo(ul);
- $('div.add-controls input').val('');
- }
- function searchElements(event) {
- let searchValue = $(this).val();
- $(ul).find('li').each((index, element) => {
- let currentListElement = element.textContent;
- let currentSearchValue = searchValue;
- if(!isCaseSensitive){
- currentSearchValue = currentSearchValue.toLowerCase();
- currentListElement = currentListElement.toLowerCase();
- }
- if (currentSearchValue.length > 0 && currentListElement.includes(currentSearchValue)) {
- $(element).css('display', 'none');
- } else {
- $(element).css('display', 'block');
- }
- });
- }
- function deleteElements(event) {
- event.target.parentElement.remove();
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement