When a checkbox is checked, hide table rows with a specific class while maintaining the background color on odd rows

When utilizing bootstrap, I want to ensure that the background color remains on the odd rows, even when hiding a specific row.

Desired Outcome: The grey background should always be present on the 1st and 3rd rows, regardless of whether the input is checked or not.

This is the code snippet:

function toggleRow() {
  if ($('#product-toggle').prop("checked")) {
    if ($('tr:has(td.status-locked)') || $('tr:has(td.product-status-whitelabel.status-locked)')) {
      $('tr:has(td.status-locked)').hide();
    }

  } else {
    $('tr:has(td.status-locked)').show();
  }
}

toggleRow();
$('#product-toggle').on('change', toggleRow);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">

<h4>If input checked hide tr with certain class(status-locked), also keep the background image on the odd tr's</h4>

<pre>
<input type="checkbox" id="product-toggle">&nbsp;<label for="product-toggle" >click me!</label>
</pre>

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
    <th>Header 4</th>
  </tr>
  <tr>
    <td>Entry First Line 1</td>
    <td>Entry First Line 2</td>
    <td>Entry First Line 3</td>
    <td class="status-locked">Entry First Line 4</td>
  </tr>
  <tr>
    <td>Entry Line 1</td>
    <td>Entry Line 2</td>
    <td>Entry Line 3</td>
    <td>Entry Line 4</td>
  </tr>
  <tr>
    <td>Entry Last Line 1</td>
    <td>Entry Last Line 2</td>
    <td>Entry Last Line 3</td>
    <td>Entry Last Line 4</td>
  </tr>
</table>

Answer №1

Here is a solution that utilizes jQuery and CSS to achieve the desired effect:

jQuery:

$("table tbody").each(function() {        
    $(this).find("tr:visible:even").addClass("even").removeClass("odd");
    $(this).find("tr:visible:odd").addClass("odd").removeClass("even");
});

function toggleRow() {
  if ($('#product-toggle').prop("checked")) {
    if ($('tr:has(td.status-locked)') || $('tr:has(td.product-status-whitelabel.status-locked)')) {
      $('tr:has(td.status-locked)').hide();
    }

  } else {
    $('tr:has(td.status-locked)').show();
  }
  $("table tbody").each(function() {        
    $(this).find("tr:visible:even").addClass("even").removeClass("odd");
    $(this).find("tr:visible:odd").addClass("odd").removeClass("even");
  });
}

toggleRow();
$('#product-toggle').on('change', toggleRow);
#example.table-striped > tbody > tr:nth-of-type(2n+1) {
background-color: transparent;
}
#example.table-striped > tbody > tr.even {
background-color: #f9f9f9;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">

<h4>If input checked hide tr with certain class(status-locked) , also keep background image on the :odd tr's</h4>

<pre>
<input type="checkbox" id="product-toggle">&nbsp;<label for="product-toggle" >click me!</label>
</pre>

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
    <th>Header 4</th>
  </tr>
  <tr>
    <td>Entry First Line 1</td>
    <td>Entry First Line 2</td>
    <td>Entry First Line 3</td>
    <td class="status-locked">Entry First Line 4</td>
  </tr>
  <tr>
    <td>Entry Line 1</td>
    <td>Entry Line 2</td>
    <td>Entry Line 3</td>
    <td>Entry Line 4</td>
  </tr>
  <tr>
    <td>Entry Last Line 1</td>
    <td>Entry Last Line 2</td>
    <td>Entry Last Line 3</td>
    <td>Entry Last Line 4</td>
  </tr>
</table>

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

Can PHP code be extracted from a file that has been loaded by other PHP code?

I am importing HTML/PHP code from an external text file using the following code: $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $line; } Is there a way for the server to interpret the PHP code within this file? Currently, I am me ...

Can you provide instructions on creating a small border at the center of an h1 heading?

What is the best way to create a small border in the center of an h1 text element? ...

What is the best way to position divs on opposite ends around another div?

I have experimented with this scenario so far, http://jsfiddle.net/BXSJe/4/ My goal is to position two divs on the left and right sides within another div, using float:left and float:right resulted in them appearing on separate lines. What I envision is ...

Displaying XML data in an HTML table

Encountered a challenge while fetching data from an external XML document using JS. Following the w3schools tutorial for AJAX XML, but faced an issue I couldn't resolve. The XML structure is as follows: <root> <document-id> <author ...

Toggle the visibility of an HTML element and dynamically change the text of a button

As a beginner in Javascript, I am experimenting with changing button text upon the show/hide of a fieldSet. However, I am encountering some issues with the button text not updating correctly. $(window).on("load", function() { $('.indoor').sl ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

What is the proper way to send an AJAX request with the data type set to

I am currently working on creating my own POST request. Below is the function I have written: function sendPost(o) { var h = new XMLHttpRequest(); h.onreadystatechange = requestComplete; function requestComplete() { if (h.readyState = ...

What is the best way to send numerous forms to a PHP page when the quantity of forms produced varies randomly?

In my project, I have a code that dynamically populates input fields at the final step based on a user's selection. Each choice can have up to 6 input fields, resulting in a maximum of 4 forms or 24 input fields in total. For data persistence, I need ...

What's the best way to combine the data from two different Instagram APIs?

Looking to display Instagram images on a webpage using UserName and Tags. Despite searching for an API that can meet my needs, I haven't found one that allows me to: Show Images based on User Account Show images based on Tags (Single or multiple) ...

Modifying JavaScript using JavaScript

I'm looking for a solution to change the URL of a button in sync with the iframe displayed on the screen using JavaScript. The challenge I'm facing is figuring out how to dynamically update the button's URL based on the iframe content. Here ...

The jQuery click event to change the color function is functioning properly, but there seems to be an issue with

I'm having trouble changing the font-family using the code below, even though the color is changing properly. What am I missing here and how can I correct it? $(document).ready(function(){ $("body").on("click",".selectableColor", function(){ ...

Minimizing the menu bar while scrolling down (bootstrap3)

Could anyone help me with creating a navigation-bar effect similar to the one seen on ? I want the bar to shrink and the logo to change after scrolling down my page. My website is built using bootstrap 3. Is there a simple way to achieve this effect with ...

Hover shows no response

I'm having trouble with my hover effect. I want an element to only be visible when hovered over, but it's not working as expected. I've considered replacing the i tag with an a, and have also tried using both display: none and display: bloc ...

Dynamic Email Design

Currently, I am working on coding an HTML email that needs to be optimized for perfect rendering on all mobile devices. While I have expertise in coding emails for desktop and ensuring compatibility with various email clients and browsers, the challenge no ...

Fading bubble div with jQuery causing input obstruction

My webpage displays transparent hint boxes that are absolutely positioned over a table of HTML inputs. However, when I use jQuery's .fadeOut(200) to fade them out, the inputs that were underneath become unclickable. Interestingly, after zooming the b ...

The event.pageY position consistently reflects the laptop screen size rather than the exact position where the click occurred

My webpage is scrollable, but the event.pageY position always corresponds to my screen size. Even when scrolling down and clicking near the top of the screen, it registers as 50px. I am currently utilizing event.pageY While this functions correctly on a ...

HTML counterpart to PHP's include for JavaScript components

I am searching for a Javascript alternative to a method I have been using in PHP. Specifically, I want to streamline the basic setup of my pages: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

Automatically resizing images in a canvas when their resolution exceeds the canvas size in HTML5

I am currently using Html5, fabric.js, and JavaScript to upload multiple images to a canvas. However, there are instances where the uploaded image size exceeds that of the canvas. I am looking for a way to ensure that the image is set to a fixed size. v ...

Search bar placeholder text appears off-center in Firefox browser

I have implemented a universal search bar on our website and it is working perfectly in Chrome and Safari. However, I am facing an issue with Firefox where the placeholder text is aligned to the bottom of the bar instead of the middle. Usually, placeholder ...

Dynamically insert letters into each row as the HTML table increases

How can I display dynamically increasing alphabets in a newly generated cell on the left side of each row, based on the option selected in a dropdown list? These alphabets will serve as bullet points or serial numbers for the text boxes in each row. View ...