Refine the table by selecting rows that contain a specific value in the href attribute of the <a> tag, and display the parent row when the child row

I've been working on filtering and searching the table below based on the href value. My code successfully filters the displayed columns in the table, but it fails to work when I search for a string within the href attribute.

For instance, if I search for "Google" in the search bar, I want to display the row that contains the href value of https://www.google.com.

I've searched everywhere for a solution, but haven't been able to find one. Can anyone offer assistance or guide me to where I might discover the answer?

Moreover, is there a way to link parent rows with child rows in the table without using the id attribute? This way, when the filter returns a child row, I also want to display the corresponding parent row.

Here's my code on JsFiddle - JsFiddle

HTML :

<body>
  <div>
    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for website..">
    <button type="submit">Search</button>
  </div>

  <table border="0" id="myTable">
    <thead hidden>
      <tr>
        <th>Name</th>
        <th>URL</th>
      </tr>
    </thead>

    <tbody>
      <tr class="header clickable-row" style="cursor:pointer" data-toggle="true">
        <td colspan="2">
          <label style="cursor:pointer">Parent Row 1</label>
        </td>
      </tr>
      <tr class="hide collapse" style="display:none">
        <td>
          <h4>Column1</h4>
        </td>
        <td>
          <a class="connectorlink" href="https://google.com" target="_blank">website</a>
        </td>
      </tr>
      <tr class="hide collapse" style="display:none">
        <td>
          <h4>Column1</h4>
        </td>
        <td>
          <a class="connectorlink" href="https://yahoo.com" target="_blank">website</a>
        </td>
      </tr>
    </tbody>
  </table>
</body>

JavaScript

$(document).ready(function() {
  $('tr.header').click(function() {
    $(this).find('span').text(function(_, value) {});
    $(this).nextUntil('tr.header').slideToggle(100, function() {});
  });
});

function myFunction() 
{
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  
  // Loop through all table rows, and hide those that don't match the search query
  for (i = 0; i < tr.length; i++) 
  {
   td = tr[i];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) 
      {
        tr[i].style.display = "";
      } 
      else 
      {
        tr[i].style.display = "none";
      }
    }
  }
}

CSS

table,
tr,
td,
th {
  border-collapse: collapse;
}

h4,
{
  margin: 0;
  padding: 0;
  font-family: Inter;
  font-style: normal;
  font-size: 20px;
  font-weight: 600;
  line-height: 28px;
}


label {
  padding: 10px 10px 10px 12px;
}

.tabledesign {
  width: 900px;
  border-collapse: separate;
  border-spacing: 0 15px;
  margin: 50px auto;
}

th {
  background: #e5e5e5;
  color: rgba(0, 0, 0, 0.84);
  font-weight: bold;
}

td,
th {
  padding: 10px;
  border: 1px solid #ccc;
  text-align: left;
  font-size: 18px;
}

#myInput {
  display: flex;
  background: #FFFFFF;
  font-family: Inter;
  /* 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: 795px;
  /* Full-width */
  height: 46px;
  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 */
}

* {
  box-sizing: border-box;
}

Answer №1

An easy solution could involve looking within the innerHTML rather than the textContent/innerText.

text = column.innerHTML;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Struggling with a Bootstrap problem in my Accordion Table

I'm working with a Bootstrap table that includes an accordion feature. My goal is to have the second row hidden by default. However, when I click to reveal it, the table shifts. How can I smoothly animate the opening without causing any shifting? ...

Having trouble with Firebug throwing a '$(' error message?

I'm encountering a peculiar issue in Firebug that I'm not experiencing in Webkit. The error being displayed is '$(' in Firebug. Here is the code that seems to be causing this problem: $.getScript("http://kylehotchkiss.com/min/?g=packa ...

Decoupling the Top Navigation Bar from the Side Navigation Bar

I currently have a top navigation bar with some menus and a side navigation bar with additional menus. I have applied styling to the vertical navigation bar, but it seems to be affecting all the navigation bars, resulting in an output as shown in the image ...

Is there a way to eliminate the gap between my menu choices?

Is there a way to eliminate the black line that appears between my menu options when using CSS hover? https://jsfiddle.net/jameskelly/3ejsu7gx/2/ a:hover { background-color: #ebebeb; font-color: black; } /* Custom styling for the ...

JavaScript & PHP: Encoding and Decoding Techniques

I am encountering an issue where only half of the HTML content is being retrieved when I send it using the POST method in Ajax and try to retrieve it using PHP. The content includes special characters. For example, I am posting the following content to an ...

Ensure that the accordion in jQuery remains open while navigating

I am currently developing a jQuery accordion feature where clicking on an 'li' element will both navigate and close the accordion. However, I need the accordion to stay open after navigating. Can someone please advise me on how to achieve this? T ...

Including a PHP file after successful AJAX request

In my div class called news, I showcase updates submitted by users. My goal is to have new updates appear at the top of the news section when a user submits one. I attempted the following approach, but it didn't produce the desired outcome. Upon form ...

I'm curious why my background generator is only altering a portion of the background instead of the entire thing

I've been attempting to create a JavaScript random background changer, but I'm encountering some challenges. The main issue is that instead of changing the entire page's background, it only alters a strip of it. Additionally, I'm curiou ...

Implement OAuth for user authentication and registration using a customized username feature on an express.js platform

Within my node.js+express.js stack, I have enabled users to register/login using both Twitter and Facebook, as well as through a native registration form. To manage this functionality, I am utilizing everyauth for handling Twitter and Facebook authenticati ...

I am attempting to create a footer with a set size, but encountering an issue

I'm in the process of developing a responsive website using Vue.js. One aspect I am working on is the footer container, which looks fine on the full screen but shrinks when the screen resolution is reduced below 1100 pixels. As shown in the images li ...

Sending data to a Bootstrap modal dialog box in an Angular application

Using span and ng-repeat, I have created tags that trigger a modal pop-up when the remove button is clicked. Within this modal, there is a delete button that calls a function. I am trying to figure out how to pass the id of the remove button to the modal ...

Why is it that only one of these functions can be operational at any given moment?

Even without the if statements, only one of the following will work at a time. To make the first one work, I have to comment out the second. <? if(isset($_POST['region'])){ echo "<script> showRecords('".$_POST['region']." ...

The command response.setHeader("Refresh", "0") failed to execute

My webpage needs to be refreshed when the user session has expired or the connection is not active. Despite trying various codes, I have been unsuccessful in achieving this. The last code snippet that I implemented is as follows: if(session.getAttribute(" ...

Updating JSON objects in jQuery with string keys

I have an array variable containing JSON data and I need to update specific values within the array using string keys. Here is a snippet of what my array looks like: { "all": [ { "image":{ "URL":"img/img1.jpeg", ...

``How can one validate a radio button within a reducer by utilizing the event object that is passed into the reducer process

In my React app, I am using the following reducer: const initialState = { genderRadio : false, ageRadio : false } const reducer = (state = initialState, action) => { switch(action.type) { case "VALI_RADIO_INP": console. ...

"Items within mui Grid do not properly align within the Grid container

I'm struggling to fit two large Grid items inside a Grid container. The Grid container needs to be 100% of the screen's height (or parent container) Grid items are large and need to fill the container while remaining scrollable Image #1 shows t ...

Tips for toggling Bootstrap 5 tabs using JavaScript instead of the button version

I am looking to switch tabs programmatically using bootstrap 5. The Bootstrap documentation recommends using buttons for tabs instead of links for a dynamic change. Here is the code I have: $("#mybut").click(function() { var sel = document.querySelector( ...

Issue with Magento: Unable to execute dataflow profiles due to Ajax Error (Uncaught TypeError: e is not a function)

Whenever I try to execute a dataflow profile in Magento, the following series of events unfold: The CSV file is uploaded successfully X rows are found A message displays: Starting ... :: saveRow (handler-method) However, a JavaScript error interrupts th ...

Is there a way to programmatically click on a link within the first [td] element based on the status of the last [td] element in the same [tr] using codeceptjs/javascript?

The anticipated outcome: Pick a random assignment from the table that has the status "Not start". Below is the table provided: <table id="table1"> <tbody> <tr class="odd1"> <td> < ...

Utilizing NLP stemming on web pages using JavaScript and PHP for enhanced browsing experience

I've been exploring how to incorporate and utilize stemming results with JavaScript and PHP pages on web browsers. After running node index.js in the VS Code terminal, I was able to retrieve the output word using the natural library: var natural = re ...