Hiding and displaying elements using jQuery for printed output

Is it possible to customize the show and hide properties of JQuery for different media types (e.g., screen and print)?

For instance, I have two divs and a button that toggles between showing and hiding one div while displaying the other. However, when it comes to printing, I want both divs to be visible. Despite defining in CSS that for printing the display should be inline-block, the currently hidden block remains hidden in the printed version. How can this issue be resolved?

Thank you, Dorijan

Here is an example:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<style type="text/css">
@media print{
    #div1{
        display:block;
    }

    #div2{
        display:block;
    }
}
</style>

<div id="div1">Div1</div>
<div id="div2">Div2</div>
<a href="#" onclick="$('#div1').hide();$('#div2').show();">Show Div1</a>
<a href="#" onclick="$('#div2').hide();$('#div1').show();">Show Div2</a>

Answer №1

To make your element visible, add a class such as .visible and then use the following code:

@media print {
    .visible {display:block}
}

Update:

Based on your revised question, it seems you will need to use the !important declaration like this:

@media print{
    #div1, #div2{
        display:block !important;
    }
}

Check out the demo where one div is hidden and try printing!

Answer №2

Take a look at this thread discussing how to print a table (or div) from an html page. There may be a better solution provided.

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 jQuery post function fails to transmit any data

I developed a basic application using jQuery's Get and Post methods to retrieve user data from a JSON file and append new users to it. In my index.html file, I have implemented a table that successfully displays users fetched via AJAX GET requests. H ...

Switch up the div background image with a click using jQuery

Hey there, I'm struggling with changing a div background image. Here's what I want to achieve: I have a set of images with an onclick event "change_color()" and also some divs with an onclick event "get_color()". What I'm trying to do is wh ...

Ways to align content in the center using Vuetify3

I have a component positioned at the top of the page, but I would like it to be centered instead. https://i.sstatic.net/wSFBP.png Something like this: https://i.sstatic.net/rfqAB.png Here is my current code: <template> <v-container class=" ...

What steps should I take to repair my jQuery button slider?

I am facing a challenge with creating a carousel of three images in HTML using jQuery. When the user clicks on the "Next" or "Previous" buttons, I want to scroll through the images one by one. However, I am struggling to hide the other images when one is d ...

Using a PHP variable to dynamically change the style sheet by connecting to a MySQL database

My goal is to have the stylesheet URLs stored in a database and be able to change the stylesheets' href using variables in HTML tags. However, when I tried the following code, nothing happened: global $theme_addresss; global $showdetail_dbtadbiruse ...

Modify the standard placement of choices in MUI

Currently, my UI is powered by MUI. I have various options displayed in the image below: https://i.sstatic.net/YKoyV.png Everything looks good so far. However, I wish to reposition the options container further down. (It is currently set at top: 64px ...

The changes made to jqGrid select editOptions may not reflect immediately

I am facing an issue with dynamically loading values into a select in a jqGrid. The challenge I am encountering is that the returned values from the server are not displayed until the user changes the row. Even though I can see the correct values being s ...

Troubleshooting problem with margin sizing in Material UI Autocomplete

My project is currently using Material-UI Autocomplete, and I've made modifications to ensure that the font and component resize appropriately when the viewport changes size. To achieve this, I used vw units for widths, heights, and font sizes. Howeve ...

"Experience a unique website layout with varying designs on desktop and mobile devices while using the React technology to enable desktop

Just starting out with React and I've noticed something strange. When I view my website on a PC, everything looks normal. However, when I switch to the desktop site on my phone, things appear differently. It seems like moving an element by 20px on mob ...

Enable hovering only on the actual content within the box, not on the empty space

Currently, I am working on creating a navigation bar using flexbox. Everything seems to be functioning correctly except for one issue: when I hover over the space between my logo and the navigation links, it changes the color of my logo to the hover state ...

Loading datatables with ajax in Liferay portlet when searching

Having an MVC Liferay portlet, I am utilizing serveResource() to handle AJAX calls in JSP The issue at hand is that I would like to trigger an AJAX function to populate datatables upon clicking the search button. I have the following code set up: <po ...

The pseudo-class for invalid input triggers CSS invalidation, even when the input field is left blank

The input property for handling invalid input is currently malfunctioning. It triggers an error even when the input field is left empty. I would like the input border to be goldenrod when it's empty, turn red if the input doesn't match the specif ...

Div vanishes once SVG Gaussian blur is applied

I'm attempting to add a Gaussian blur effect to an element that contains some child nodes with content. In Chrome, I successfully applied the blur using the following style: -webkit-filter: blur(2px); Unfortunately, Firefox does not support thi ...

Troubleshooting a Hover Problem in Firefox

Chrome is rendering the following code perfectly fine, but Firefox seems to be having trouble applying the hover effect. HTML <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="stylesheet.css"/> ...

jQuery and ajax not functioning properly with disabled button

After a user clicks the submit button in an HTML form, I am trying to disable the button. However, the form redirects to the next page without disabling the button. The ideal process should be: User clicks the submit button Validation is performed Submi ...

What's inside the navigation bar, icons, and content cards? (Using HTML and Bootstrap)

Having a few issues that need resolving: Unable to display the contents of my navbar after the navbar-brand section. Looking to add a home button, about section with a drop-down menu including: 'Our mission', 'our team', & ...

A JointJS element with an HTML button that reveals a form when clicked

How do I bind data to a cell and send it to the server using the toJSon() method when displaying a form on the addDetail button inside this element? // Custom view created for displaying an HTML div above the element. // ---------------------------------- ...

Stop images from being stored in the database instead of text characters

Using a proxy, I attempt to parse some HTML. One of the elements is retrieved using jQuery: var site = 'http://www.kartabu.com/pl/index.php?filter=random' var url = 'http://localhost/taboo.blue-world.pl/admin/proxy.php?url=' + encodeUR ...

Interactive website information

Currently, my website features a jQuery UI navigation system and utilizes ajax to dynamically update content by swapping div ids from one html file to another. However, this method still involves multiple HTML files which can become cumbersome. I am curiou ...

What could be causing the JQuery date picker to fail to load on the initial ng-click event?

I am encountering an issue with a JQuery UI date picker that is loaded through ng-click using the code below: Input: <input type="text" id="datepicker" autocomplete="off" ng-model="selectedDate" ng-click="showDatepicker()" placeholder="(Click to sele ...