Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filter a table based on combibox in js

unction filterTable() {
  // Variables
  let dropdown, table, rows, cells, country, filter;
  dropdown = document.getElementById("countriesDropdown");
  table = document.getElementById("myTable");
  rows = table.getElementsByTagName("tr");
  filter = dropdown.value;

  // Loops through rows and hides those with countries that don't match the filter
  for (let row of rows) { // `for...of` loops through the NodeList
    cells = row.getElementsByTagName("td");
    country = cells[1] || null; // gets the 2nd `td` or nothing
    // if the filter is set to 'All', or this is the header row, or 2nd `td` text matches filter
    if (filter === "All" || !country || (filter === country.textContent)) {
      row.style.display = ""; // shows this row
    }
    else {
      row.style.display = "none"; // hides this row
    }
  }
}
<select id="countriesDropdown" oninput="filterTable()">
  <option>All</option>
  <option>Sweden</option>
  <option>Germany</option>
</select>

<table id="myTable">
  <tr><th>Name</th><th>Country</th></tr><!-- header row uses 'th' instead of 'td' -->
  <tr><td>Inga</td><td>Sweden</td></tr>
  <tr><td>Helena</td><td>Sweden</td></tr>
  <tr><td>Hans</td><td>Germany</td></tr>
  <tr><td>Anna</td><td>Germany</td></tr>
</table>
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to increase the window size in nightmare 
Javascript :: how ot make a background color faor evaluationbutton in flutter 
Javascript :: Div draggable x axe only 
Javascript :: parsely ignores hidden field 
Javascript :: dockument anywhere click fucntion in js 
Javascript :: tooltip in javasrript UI 
Javascript :: delete class through the dom 
Javascript :: js.l17 
Javascript :: java jsp attribute qualified names must be unique within an element 
Javascript :: js let vs var performance 
Javascript :: angular key value pipe compareFn example 
Javascript :: react state deconstructed 
Javascript :: check event target jquery outside 
Javascript :: anonymous auto invoke is not a function 
Javascript :: random order of buttons on refresh in vanilla js 
Javascript :: crypto 32 characers encryption node js 
Javascript :: server sent events node js + github 
Javascript :: warn user before leaving page angular 
Javascript :: nodejs json data serving 
Javascript :: how to make an object stop at the end of your canvas p5js 
Javascript :: javascript short syntax get element 
Javascript :: cadena promesas javascript 
Javascript :: page slug vuejs 
Javascript :: paamayim nekudotayim 
Javascript :: how create array with names of files in folder nodejs 
Javascript :: content disposition attachment javascript fetch download "excel" 
Javascript :: Angular UI datepicker is getting wrong date 
Javascript :: java script num toSting syntax eror 
Javascript :: how to create duplicate key array in javascript 
Javascript :: jquery dialog button text set by variable 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =