I am currently working on implementing a dropdown select menu that helps filter an HTML table based on the user's choice. My goal is to utilize the URL of the page to automatically pre-select an option in the dropdown. For instance, if the URL looks like this: , then I want the dropdown option to be set to Product A. I have shared the HTML and CSS code below for reference. If possible, I would prefer to achieve this functionality using 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 have attempted various solutions found on Stack Exchange but have not been successful so far. I am hesitant to share any code here as nothing has worked for me yet.