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

Is it possible to use reactjs and react-router to showcase either a component or {this.props.children}?

Here's the scene: I have multiple components where certain words can be clicked to link to a reference page/component. If the highlighted words are not clicked, the component is displayed as is (and there are many of them with this feature and a menu ...

Utilize the power of Nuxt and Vue 3 to create sortable columns in a table, with the ability to only change the icons on the sorted column

I am facing a challenge with my sortable table icons. Whenever I click on an icon to sort a column, all the other icons also change direction. I understand that this is happening because I am toggling the visibility of icons based on the currentSortDir sta ...

Exploration of DOM elements using jQuery

There is a table with two rows: <tr class='data'> <td class='img'><img class='thumbnail' src='ProductImages/img_1894_72.jpg'/></td> <td class='bc_img'><i ...

jQuery Ajax form submission encountering issues

I created a form dynamically with the help of jQuery. Here is the code: <form id="editorform" accept-charset="utf-8" method="post" name="editorform" action="menuupdate"> <div> <input id="updateItem" type="submit" value="Update"& ...

Firebase Firestore is returning the dreaded [object Object] rather than the expected plain object

I've created a custom hook called useDocument.js that retrieves data from a firestore collection using a specific ID. However, I'm encountering an issue where it returns [object Object] instead of a plain object. When I attempt to access the nam ...

Looking to retrieve all information related to a specific product_id by searching and selecting it in the form field?

I'm currently working on an Inventory management system and facing a challenge with the search query functionality. The tables I have set up are: products product_purchases product_sales My goal is to retrieve product details from the "products" t ...

Why does the container overflow when the child is floated in CSS?

Can you explain why a parent div element stops adjusting its height when its child is floated? Here is the HTML snippet: <!DOCTYPE html> <html> <head> <title>test</title> <link rel="stylesheet" type="text/css" href="in ...

Leveraging a PHP variable to dictate the selection of an HTML dropdown menu

Upon loading the page, a select box is set with a selected value based on a variable. Two IF statements are used to check the variable and return results. Below is an example of the code: <select id="satbreak1" name="satbreak1"> <?php if ($trimme ...

Understanding Semantic Versioning (Semver) - A guide to properly Semvering major functional enhancements while maintaining backwards compatibility

It is my understanding that when using X.Y.Z, X is only changed for breaking updates while Y is reserved for backward compatible functional modifications. Therefore, can I infer correctly that even if my update involves a significant enhancement to functi ...

Create a line connecting two divs using jQuery's DOM manipulation capabilities

Looking to connect two divs with a straight line, I stumbled upon jQuery DOM line, which appears to offer a more streamlined solution compared to jsPlump. I attempted to incorporate it into my code, but unfortunately, it's not working as expected. Be ...

Incorporating variable parameters into a URL for asynchronous jQuery calls

Seeking to comprehend the intricacies of a $.ajax call: var url = "/api/followuser.php/" + userID ; $.ajax(url, { 'success': function(data) { //do something } }); This specific ajax call necessitates s ...

ES6 Class Directive for Angular 1.4

I am currently exploring the possibility of incorporating a directive from version 1.4 and adapting it to resemble a 1.5 component. By utilizing bindToController and controllerAs, I aim to integrate the controller within my directive rather than using a se ...

Displaying the second div once the first div has loaded, then concealing the first div

Current Approach: There are two divs occupying the same space, with div1 set to display:block and div2 set to display:none When a tab is clicked, jQuery hides one div over a period of 2000ms and reveals the other div. Challenge: The goal is for the ...

Troubleshooting jqGrid Error with jsonReader when JSON does not contain 'rows' information

When using jqGrid with Fusion Tables, everything works smoothly if the JSON returns results. However, a problem arises when there are no results because obj.rows is non-existent. This leads to the page breaking while attempting to check the length. Is ther ...

Querying data via AJAX from a specific Laravel route to retrieve a JSON encoded array filled with dates to populate ChartJS labels

My Laravel 5.7 project includes ajax and ChartJS functionality. Upon page load, an ajax call is made to "action_route", which retrieves the labels for the ChartJS. The PHP function json encodes an array of labels, which are then decoded by the ajax call. ...

Show just a single error message if there are two validation errors present

In my AngularJS timepicker, users can choose multiple time segments for each day. The code has validation to detect duplicates and overlapping time segments. For example, entering 11:00am - 12:00am twice will trigger two error messages: 'Overlapping t ...

Navigating through a modal without affecting the main page's scroll position

My webpage has some content, and I also have a modal that pops up on top of it. The problem I'm facing is that when I try to scroll the contents within the modal, only the contents behind it scroll instead. Below is the CSS code: html, body { b ...

Weird Android CSS problem: mysterious white overlay appearing in all browsers, both zoomable and touch-sensitive

After testing my website in various browsers, I discovered that while it looks fine on most platforms, there is a persistent issue on Android devices. The problem occurs when the user scrolls down around 30% of the page - a white overlay appears and exten ...

Limiting Velocity in a Two-Dimensional Spacecraft

Like many others diving into the world of programming, I decided to challenge myself with a spaceship game project. At this point, I have successfully incorporated parallax stars and other essential features expected in a space-themed game. The spacecraft ...

Exploring Database with NodeJS, Express, and HTML

My goal is to create a basic web application using node.js where users can input data into a search bar and have that input sent to the server for database query. While I've successfully set up and connected my database, here's a glimpse of my co ...