Advertisement
elena1234

How to check for className and remove it ( JavaScript)

Nov 5th, 2021
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let searchButton = document.getElementById('searchBtn');
  3.     let searchField = document.getElementById('searchField')
  4.  
  5.     searchButton.addEventListener('click', function () {
  6.         let searchFieledValue = searchField.value.toLowerCase();    
  7.         removeSelectedRow();
  8.         selectFindedRow(searchFieledValue);
  9.     })
  10.  
  11.  
  12.     function removeSelectedRow() {
  13.         let selectedRows = document.getElementsByClassName('select');
  14.         while (selectedRows.length > 0) {
  15.             selectedRows[0].classList.remove('select');
  16.         }
  17.     }
  18.    
  19.     function selectFindedRow(searchFieldValue) {
  20.         searchField.value = '';
  21.         let tableData = document.querySelectorAll('tbody td');
  22.         for (const td of tableData) {
  23.             if (td.innerHTML.toLowerCase().indexOf(searchFieldValue) !== -1) {
  24.                 let row = td.parentElement;
  25.                 row.classList.add('select');
  26.             }
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement