Generate HTML table with CSS dynamically applied through jQuery

After performing some calculations and applying CSS rules using the .css() function in jQuery to color the background of a table, I am facing an issue when trying to print the page. The printed page appears in black and white without retaining any styling effects.

I need assistance on how to print the table while preserving its appearance. I am aware that special stylesheets can be created for printing purposes, but I currently lack the knowledge on how to do so. Any guidance on this matter would be greatly appreciated.

This is the HTML code for the table:

<table id='tbl1' style="width:100%">
  <tr>
    <th>Name</th>
    <th>Location</th> 
    <th>Amount in kg</th>
  </tr>
  <tr>
    <td>Whole wheat</td>
    <td>Line 1</td>
    <td>1237</td>
  </tr>
  <tr>
    <td>Whole wheat</td>
    <td>Line 2</td> 
    <td>1341</td>
  </tr>
</table>

Below is a code example demonstrating how CSS rules are applied based on certain conditions:

table = $('#tbl1').find('td:nth-child(3)').each(function() {
  val = parseInt($(this)[0].innerText, 10);
  if (val > 1300) {
    $(this).css({"background-color": "green"});
  }
  else {
    $(this).css({"background-color": "red"});
  }
}); 

Here is a fiddle example showcasing the scenario:

https://jsfiddle.net/toofle/jng2h9rp/

Your assistance is highly appreciated. Thank you!

Answer №1

Include the media="print" attribute in your CSS style tag to apply styles specifically for printing.

<style media="print">
   // Add your CSS code here
</style>

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

The div is not displaying the background image as intended

.cover-image { margin-bottom: 0px; height: 350px; color: white; text-shadow: black 0.3em 0.3em 0.3em; } <div class="cover-image" style="background: url('~/images/hdpic.jpg') no-repeat; background-size:cover"> </div> I&apo ...

Solve the problem with SCSS at the component level in NextJS

I've decided to transition my regular React app to Next.js. In the past, I would simply import SCSS files using: import from '.componentName.scss' However, now I need to import them using: import style from 'componentName.module.scss ...

Assign an identifier to the HTML helper Html.EditorFor

When using a textbox created with the HTML helper "@Html.EditorFor", there may be a need to perform some action with JavaScript on its change event. In order to do this, an ID needs to be set for the textbox. For example, if you need to set a string value ...

Create a responsive DIV element within a website that is not originally designed to be responsive

I am looking to optimize the positioning of an ad div on my non-responsive website. The current setup has the ad displayed at 120x600 pixels, always floating to the right of the screen. While this works well for desktop or large devices, the display become ...

Use a foreach loop to iterate over an array of dates and display them in individual tables

I am currently working on structuring my foreach loop to iterate through all the dates in my time stamp arrays. The goal is to display the results in 5 separate tables, each pertaining to one of the dates. Below is the function I've implemented to re ...

What is the best way to interact with Redis without using any external modules?

I am curious about the communication process between the node redis wrapper and the RESP (REdis Serialization Protocol) database. Here is a simple example: const redis = function(uri) { this.client = '' // How do we establish a connection wit ...

Can you create a dynamic visual display using HTML5 Canvas to draw lines in a circular pattern that react

I have successfully implemented drawing lines around a circle using the AudioContext API. However, I am facing an issue with the lineTo function, as the line only grows and does not shrink. My inspiration for this project comes from the audio visualizer fo ...

Setting up a Node.js project in your local environment and making it

I need help installing my custom project globally so I can access it from anywhere in my computer using the command line. However, I've been struggling to make it work. I attempted the following command: npm install -g . and some others that I can&ap ...

Is it possible to include multiple eventTypes in a single function call?

I have created a function in my service which looks like this: public refresh(area: string) { this.eventEmitter.emit({ area }); } The area parameter is used to update all child components when triggered by a click event in the parent. // Child Comp ...

What is the best way to implement OTP expiration time in Next.js using Firebase?

Could anyone please help me with setting the OTP expire time in Next.js using Firebase? I have searched through the Firebase documentation but haven't been able to find a solution to my issue. Below is the code I am using to send the OTP: const appV ...

Endless loops: How React JS components can render indefinitely

Every time I try to render a screen with the Bar component, it seems to get stuck in an infinite loop without even passing any data. I tested importing react-chartjs-2 and that worked fine, loading just once. However, the other bar chart keeps rendering co ...

Flask Socket-IO - Instant Messaging App with Real-Time Communication, Socket Functionality Not Operating as Expected

I've been dedicating a good amount of time to this flask project, working on incorporating Flask-SocketIO along with JavaScript SocketIO to enable real-time messaging capabilities. Here's the Python section of the code: ## Setting up Socket IO f ...

Hover over CSS sprite

My anchor tag looks like this: <a href="#"> <i class="ico01"></i> <span>Text</span> </a> The ico01 class applies an image using CSS sprite. I want to change the background color of the entire content inside the a ...

What causes <input> elements to vertically center when using the same technique for centering <span> elements? (The line-height of <input> does not match its parent's height)

Important: The height of <div> is set to 50px, and the line-height of <span> is also 50px When 'line-height' is not applied to the <span>, all elements align at the top of the parent. However, when 'line-height: 50px&apo ...

Confirming the username's accuracy through an external API as part of the registration procedure

My latest project involves creating a web application specifically for Fortnite players looking to connect with other gamers. The concept is simple: users can register, log in, post, and comment within the platform. I have successfully designed the fronten ...

Start fresh with the count for every individual table

In my HTML document, I have multiple tables with columns that have classes "valuation" and "valuation_all". If one cell in the "valuation" column contains the text "FAIL", then the corresponding cell in the "valuation_all" column should display "FAIL"; oth ...

The issue of passing state in React Router v4 Redirect unresolved

I have a specific private route, /something, that I only want to be accessible when logged in. I've used Redirect with the state parameter set, however, when I try to access it at the destination, location.state is showing as undefined. Here is how I ...

Using jQuery AJAX to dynamically update two sections of a webpage by executing scripts embedded in the server response

I'm currently utilizing jQuery AJAX to load and update a specific section of my webpage. Below is the jQuery function in my main page: function updateCategories(){ catList = $('#cat_list'); catList.hide(); //sending the post re ...

Tips for saving data to a file using the frontend

Is it possible for draw.io to write directly to a local file from my browser without the need for any server app or browser extension? I'm not referring to simply downloading a file, but actually writing to a file. For example, when creating a new dia ...

Merging HTML Array with jQuery

I am working with input fields of type text in the following code snippet: <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > ...