There are two print buttons styled with unique CSS designs

I am currently working on an application using Django. Within one of my HTML pages, I need to display a receipt. There are two print buttons available: print_btn and print_btn_new, each with distinct styles. To accommodate these styles, I have created two separate stylesheets called print.css and print_new.css.

In the header section

<link rel="stylesheet" href="{% static 'css/print.css' %}" id="printCss" media="print">

Everything appears to be working correctly. However, when the user clicks on print_btn_new, the printed receipt does not reflect the intended CSS styling.

For the print_btn_new

<button id="print_btn_new" onclick="printNewOnClicked();" name="button">Print New</button>

The printNewOnClicked() function :

function printNewOnClicked(){
        document.getElementById('printCss').href = '{% static 'css/print_new.css' %}';
        alert(document.getElementById('printCss').href);
        window.print();
    }

Interestingly, the alert message correctly displays the URL for print_new.css. Despite this, manually inputting the URL from the alert box into the address bar results in displaying the correct file.

Answer №1

It seems like there is an issue with the JavaScript code you copied. In the second line, make sure to escape the quotation marks like this:

document.getElementById('printCss').href = '{% static \'css/print_new.css\' %}';

Additionally, simply changing the href in this manner may not update the CSS styles on your page without a reload.

Answer №2

When you assign one or more classes to the HTML tag, it allows you to create distinct CSS rules for different sets of rules effortlessly. The process of writing these rules in plain CSS can be tedious; however, tools like LESS/SASS can make it less repetitive and time-consuming. Moreover, by using this technique of adding classes to the HTML tag, it opens up possibilities for other uses by different individuals!

var _global_printCSSnames = [ 'funky' ];
function printPage(mode){
    let doc = document.querySelector('html');
    //doc.className = mode !== null ? `${mode}-printstyle` : ''; // just for our print css switch
    _global_printCSSnames.forEach( pmode => { doc.classList.remove( `${pmode}-printstyle` ) } ); if(mode!==null) doc.classList.add( `${mode}-printstyle` ); // if other classes may be set on HTML tag
    window.print();
}
html,body{ background-color: #444; color: #fff; }

@media(min-width:50em){
    html,body{ background-color: #aaa; color: #444; }
}

@media(min-width:80em){
    html,body{ background-color: #444; color: #fff; }
}

@media print{
    html,body{ background-color: #fff; color: #000; }
    html.funky-printstyle,html.funky-printstyle body{
        background-color: #fff; color: #00f;
    }
}

.buttonBox{ display: flex; flex-direction: row; justify-content: space-around; }
.buttonBox>*{ flex: 0 1 auto; width: 9em; height: 2em; margin: 4px 8px; }

button{
    background-color: #333;
    color: #0f8;
    border-radius: 4px;
    border: 2px solid #888;
    box-shadow: 4px 8px 4px rgba( 128, 128, 128, .5 );
    transition: all ease-in-out .3s;
}
button:hover{
    background-color: #555;
    color: #0ff;
    text-shadow: 1px 1px 0 1px #000, -1px -1px 0 1px #fff;
    cursor: pointer;
}
<h1>Lorem ipsum…</h1>
<p>Zwei flinke Boxer jagen die quirlige Eva und ihren Mops durch Sylt. Franz jagt im komplett verwahrlosten Taxi quer durch Bayern. Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich. Vogel Quax zwickt Johnys Pferd Bim. Sylvia wagt quick den Jux bei Pforzheim. Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark. "Fix, Schwyz!" quäkt Jürgen blöd vom Paß. Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich. Falsches Üben von Xylophonmusik quält jeden größeren Zwerg. </p>
<p>Heizölrückstoßabdämpfung. Zwei flinke Boxer jagen die quirlige Eva und ihren Mops durch Sylt. Franz jagt im komplett verwahrlosten Taxi quer durch Bayern. Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich. Vogel Quax zwickt Johnys Pferd Bim. Sylvia wagt quick den Jux bei Pforzheim. Polyfon zwitschernd aßen Mäxchens Vögel Rüben, Joghurt und Quark. "Fix, Schwyz!" quäkt Jürgen blöd vom Paß. Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich. Falsches Üben von Xylophonmusik quält jeden größeren Zwerg. </p>
<div class="buttonBox">
    <button onclick="printPage(null)">print (regular)</button>
    <button onclick="printPage('funky')">print (funky)</button>
</div>

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 value of 'null' was passed as a parameter in a Mongoose query for the function 'findOne()'

After spending countless hours trying to resolve an issue, I find myself at a standstill. Let's dive right in. Using the latest versions of express and mongoose In my route.js file, I have the following query written: Note .findOne({"user": req ...

Dragging an image within a div using JQueryUI

Something strange is happening with my code. I am utilizing JQueryUI to enable dragging of div elements that contain an image tag. The issue arises when I start dragging the div - the image, which was perfectly centered within the div while stationary, now ...

Utilizing Ajax for Efficiently Updating a Singular Field within a Designated Object

How can I use Ajax to Update a Single Field in a Specific Object? I have a table in my postgres database with numerous records. I am interested in using a jquery Ajax request to update just one field in a particular object within that table. Is it possibl ...

setTimeout in CSS animation causing issues

I recently created an animation using CSS3. The source code is quite simple. It involves displaying text and then making it disappear after 4 seconds. Below you can find the current source code: const intro1 = document.getElementsByClassName('intro ...

"Consistent Behavior: jQuery ajax always triggers fail event, even when success is expected

My web page relies heavily on AJAX requests. Take a look at the following code snippet: function fetchSpecificData(start,end){ var link = createLink('open', start, end); getAjaxResponse(link).done(function(data) { //store data in ...

Using Ajax to update multiple text field rows with unique values

I have a question regarding utilizing Ajax to update the second text field based on the input from the first text field. I am looking to create multiple rows using a for loop, with each row having its own set of values. HTML <form style="text-align:c ...

The transformation of a class-based component into a functional one is proving to be ineffective

I am attempting to transform my class-based component into a functional one, but I am struggling with passing two parameters in one onClick function without relying on set state. Additionally, I want to avoid creating multiple extra functions as it would i ...

What is the significance of using composability over the deprecated method in Reactjs Material-UI menuItems?

I have taken over a project that was built using an older version of react, and I am currently in the process of updating it. However, I encountered console errors right off the bat. Error : bundle.js:6263 Warning: The "menuItems" property of the "Left ...

Is there a way to trigger the "day click" event when the "Event Click" is clicked on in Jquery full calendar?

When using a jQuery calendar, there are options for day click and event click functionality. I am specifically trying to only handle the "day click" event, even if an "event" is clicked. In order to achieve this, I have commented out the event click functi ...

Adjust the dimensions of the main svg while keeping the proportions of all elements contained within

Is there a way to adjust the size of the outermost svg while keeping the size of the elements inside the svg unchanged? I've searched online but couldn't find a solution to this specific issue. To provide context and avoid the x-y problem, let ...

Creating a basic JSON array using the push method

When adding comments to a blog post using the line: blogpost.comments.push({ username: "fred", comment: "Great"}); The JSON structure of the comments section will look like this: "comments":[{"0":{"username":"jim","comment":"Good",},"1":{"username":"fre ...

Look through the contents of each child within a div to locate specific text, then conceal any that do not include it

I want to dynamically hide divs that do not contain the text I specify. Here is the code I have tried: var $searchBox = $('#search-weeazer'); $searchBox.on('input', function() { var scope = this; var $userDivs = $('.infor ...

Stop the auto-scroll feature when the user begins manually scrolling upwards, and resume the auto-scrolling when the user continues

I've been trying to solve this issue for the past couple of days without success, so I'm reaching out for help. I have a parallax website and I want it to automatically scroll when the user scrolls down (which is currently working). However, I al ...

A guide on changing a class dynamically in a Vuejs component by utilizing a variable from Laravel

I have been struggling with this issue and attempted multiple times to find a solution. My goal is to toggle an id using Vuejs based on a boolean value passed from Laravel. I initially achieved this without components, but encountered a problem where updat ...

What is the best way to apply margins to a nested div element in relation to its parent div element using CSS?

My Coding Dilemma: #b { width: 700px; height: 150px; margin-top: 10%; margin-left: 20%; text-align: center; border: 2px solid black; } .block { width : 10%; height : 10%; border: 2px solid black; padding: 40px 40px; margin: inheri ...

Unwanted transparency issue in MaterialUI (MUI) BottomNavigation

Greetings fellow hobby developer! I recently embarked on a project using create-react-app and incorporated MUI dependencies. One feature I added was a fixed BottomNavigation, which you can view in action here. Interestingly, in CodeSandbox, the BottomNavi ...

Heroku NodeJS - Page Not Found: Error 404

I recently set up a Heroku server with BootBot running on it, but I'm facing challenges while trying to render an HTML page from it. Here is what I have in my code: var app = express(); var path = require('path'); app.use(express.static(pat ...

Creating a standalone Wordpress child theme with its own CSS

After creating a child theme based on the responsive i-transform theme from http://templatesnext.org/itrans/, I found myself continuously undoing the i-transform CSS, which is taking up a lot of my time. However, if I remove the entire parent stylesheet, t ...

Can you tell me the appropriate type for handling file input events?

Using Vue, I have a simple file input with a change listener that triggers the function below <script setup lang="ts"> function handleSelectedFiles(event: Event) { const fileInputElement = event.target as HTMLInputElement; if (!fileInp ...

Ensure that a distinct cross button is included within the text or search input and positioned to float alongside the text for

Is there a simple method to include a clear button that hovers after the text in an input field when using a search type input? <input type="search"> ...