I’m using a dropdown select menu which filters an html table based on the user’s selection. I would like to use the URL of the page to select one of the options. For example, if I go to the following URL: https://www.mywebsite.com/productsDropdown=producta
I would want the dropdown option to be Product A.
I’ve included the HTML and CSS below if it helps
If possible I’d like to be able to use Javascript
<div class="filter-section">
<div class="filter">
<p class="filter-name">Product</p>
<select class="dropdown" id="productsDropdown" oninput="filterTable()">
<option>All</option>
<option>Product A</option>
<option>Product B</option>
<option>Product C</option>
</select>
</div>
</div>
<!-- filter section end -->
<!-- table start -->
<table id="myTable">
<tr class="header">
<th style="width: 20%">Name</th>
<th style="width: 40%">Description</th>
<th style="width: 20%">Product</th>
</tr>
<!-- Access Requests start -->
<!-- row start -->
<tr>
<td>
<a class="request-link" href="#" target="_blank">Request A</a>
</td>
<td>Sit amet consectetur adipisicing elit.</td>
<td>Product A</td>
</tr>
<!-- row end -->
<!-- row start -->
<tr>
<td>
<a class="request-link" href="#" target="_blank">Request B</a>
</td>
<td>Modi placeat quos impedit sit optio doloremque veniam expedita?</td>
<td>Product B</td>
</tr>
<!-- row end -->
<!-- row start -->
<tr>
<td>
<a class="request-link" href="#" target="_blank">Request C</a>
</td>
<td>Lorem ipsum dolor, sit amet consectetur adipisicing elit.</td>
<td>Product C</td>
</tr>
<!-- row end -->
</table>
<!-- table end -->
#myInput {
background-image: url("https://www.w3schools.com/css/searchicon.png"); /* Add a search icon to input */
background-position: 10px 12px; /* Position the search icon */
background-repeat: no-repeat; /* Do not repeat the icon image */
width: 100%; /* Full-width */
font-size: 16px; /* Increase font-size */
padding: 12px 20px 12px 40px; /* Add some padding */
border: 1px solid #ddd; /* Add a grey border */
margin-bottom: 12px; /* Add some space below the input */
}
#myTable {
border-collapse: collapse; /* Collapse borders */
width: 100%; /* Full-width */
border: 1px solid #ddd; /* Add a grey border */
font-size: 18px; /* Increase font-size */
/* display: block;
overflow: auto;
height: 500px; */
}
#myTable th,
#myTable td {
text-align: left; /* Left-align text */
padding: 12px; /* Add padding */
}
#myTable tr {
/* Add a bottom border to all table rows */
border-bottom: 1px solid #ddd;
}
#myTable tr.header,
#myTable tr:hover {
/* Add a grey background color to the table header and on hover */
background-color: #f1f1f1;
}
.filter-section {
display: flex;
margin-bottom: 6px; /* Add some space below the filters */
gap: 12px; /* Add some space between the filters */
}
.filter-name {
font-size: 14px; /* Increase font-size */
color: #666666;
}
.dropdown {
border: 1px solid #ddd; /* Add a grey border */
border-radius: 5px;
font-size: 16px;
padding: 1px; /* Add padding */
}
.request-link:link,
.request-link:visited {
text-decoration: none;
color: #4050c7;
}
.request-link:hover,
.request-link:active {
color: #4050c7;
}
I’ve tried several solutions I found on Stack exchange and have so far been unsuccessful. I’m hesitate to paste any code here because so far nothing has worked.
2
Answers
I ended up using the following code:
I had some trouble with the window.onload so I ended up just removing it and calling it a different way. But this should work.
An
option
should always have a value! If it has, you can useSelectElement.value
to always get the value of the selected option.You need to add an
addEventListener
to theselect
and listen forchange
events. In modern JS you should avoid usingon
-attributes to keep HTML and JS independent.You pretty much need 2 functions (or a class with 2 methods) to either set or remove the search filter. You can use an
if/else
-statement for that. You also need a CSS class to actively hide a table row.The best tool you have is
classList.toggle()
which can add or remove a class based on a condition. This is especially helpful if you change the filter.For that functionality, an
URLSearchParam
is not required but can be added and I did so: