Preview not showing CSS changes properly

1) I am encountering an issue with displaying CSS in a form preview tab. Despite setting the CSS, it does not reflect on the fields after clicking the preview button.

2) Are there alternative methods to open the tab in a new window rather than opening it directly?

/*form preview*/
function PrintPreview() {
  var toPrint = document.getElementById('from_privew');
  var popupWin = window.open('', '_blank', 'width=1000,height=1000,location=no,left=200px');
  popupWin.document.open();
  popupWin.document.write('<html><title>::Print Preview::</title><link rel="stylesheet" type="text/css" href="Print.css" media="screen"/></head><body>')
  popupWin.document.write(toPrint.innerHTML);
  popupWin.document.write('</div></body></html>');
  popupWin.document.close();
}
@media print{
    input[type="text"],
    input[type="email"] {
      border: 1px solid #000;
      padding: 05px;
      border-radius: 05px;
    }

    input[type="submit"],
    a {
      background: #0088cc;
      border: none;
      border-radius: 05px;
      padding: 08px 25px;
      color: #fff;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div id="from_privew">
  <form>
    <input type="text" name="name">
    <input type="email" name="email">
    <input type="submit" name="submit">
    <a href="#" onclick="PrintPreview()">Preview</a>
  </form>
</div>

Answer №1

Separate your CSS code into a file named Print.css and remove the @media print section:

input[type="text"],
input[type="email"] {
    border: 1px solid #000;
    padding: 05px;
    border-radius: 05px;
}

input[type="submit"],
a {
    background: #0088cc;
    border: none;
    border-radius: 05px;
    padding: 08px 25px;
    color: #fff;
}

Next, update your JavaScript code to include a reference to the new CSS file:

function PrintPreview() {
    var toPrint = document.getElementById('from_privew');
    var popupWin = window.open('', '_blank', 'width=1000,height=1000,location=no,left=200px');
    popupWin.document.open();
    popupWin.document.write('\
        <html> \
            <head> \
                <title>::Print Preview::</title> \
                <link rel="stylesheet" type="text/css" href="Print.css" media="screen"/> \
                <link rel="stylesheet" type="text/css" href="style.css" media="screen"> \
            </head> \
            <body> \
                <div>')
    popupWin.document.write(toPrint.innerHTML);
    popupWin.document.write('\
                </div> \
            </body> \
        </html>');
    popupWin.document.close();
}

However, whether the page opens in a new window or tab is determined by the browser, so there may not be much you can do about that.

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

Having trouble with webpack displaying CSS background images?

I've set up the html boilerplate using webpack. I'm currently encountering an issue where calling an image in my scss file like "background: url('/img/img.png');" isn't working. I've included the webpack file and folder struct ...

Leverage JSON data within an AngularJS controller

I have a JSON list in Angular that looks like this: vm.allnews = actualNews; After checking it with console.log, I can see that it's working fine and I am able to retrieve all news from the array list. Each news item has a title which I can display ...

What is the most effective method for updating a basic div element using AJAX?

I'm struggling with updating the element that shows the value in real-time from the database using AJAX. This application is designed to take a number, increase it by 1, save the new value, and then display the updated value live for the user. It&apo ...

Moving object sideways in 100px intervals

I'm struggling to solve this issue. Currently, I have multiple div elements (each containing some content) that I need to drag and drop horizontally. However, I specifically want them to move in increments of 100px (meaning the left position should b ...

Utilizing AJAX to integrate the Google Maps Direction API

Currently, I am developing a small website that utilizes the Google Maps API and AJAX to load directions based on selected locations from a dropdown menu. The database stores latitude and longitude coordinates of various locations, which are retrieved via ...

Utilizing Angular to apply multiple ng-repeat directives with multiple filters

I am working on a project that involves multiple ng-repeat lists with several filters. Currently, I am using (ex:A.value) if (ex:B.value), but I would like to implement multiple filters. The filters I want to incorporate are recommend_search, skill_searc ...

What is causing the border-bottom in these inline divs to not display when using a fractional height? (Specifically in Chrome)

Here is a jsfiddle I'd like to share with you: https://jsfiddle.net/ionescho/4d07k799/4/ The HTML code: <div class='container'> <div class="row"></div><div class="row"></div><div class="row"></div> ...

Ways to obtain every image placed onto an element

Using the img tag within div.image-block sets a background. Images can be added to .block3 through drag and drop. Is there a way to create a container that includes all elements from .image-block? <style> .image-block { position: relat ...

While troubleshooting the app, I encountered an error that says: "The property 'answers' cannot be read as it is undefined."

Everything was going smoothly with my app until it suddenly crashed, displaying the error message "Cannot read property 'answers' of undefined". Let's take a look at the specific piece of code causing the issue: function mapStateToProps({ ...

Utilizing two imports within the map() method

I have been considering the idea of incorporating two imports within map() in my React code. This is how my code looks: {this.state.dataExample.map(item => ( <ItemsSection nameSection={item.name} /> item.dat ...

What is the best way to maintain the CSS3 animation state following ng-animate?

Attempting to alter the state of a div using ng-animate along with CSS3 transitions. The animations are functioning well with this particular CSS, except for the issue where the div loses its size and color once the animation is completed. .fade-hide, .f ...

The setInterval function in JavaScript fails to update the result

I have successfully implemented a countdown function that is working as expected. // Set the date we're counting down to var countDownDate = new Date("<?= $stop_datejs ?>"); // Update the count down every 1 second var x ...

Avoiding incorrect AJAX requests made by the user

I created a webpage that grants users X points when they click a button. When the button is clicked, it triggers an AJAX request using JQuery to a PHP file that handles awarding the points using POST method. However, since this is client-side code, the pa ...

Why Does my Overlay Keep Bouncing when Using JQuery to slideDown / slideUp Text on Image?

Can anyone help me replicate the text overlay effect that is seen when hovering over an image on The Guardian website? I have almost got it working, but whenever my mouse moves over the trail-text area, it starts bouncing (slideDown slideUp) repeatedly. I ...

Pagination with React Material UI is a user-friendly and visually

Requirement Within the Material UI framework, I need to implement pagination functionality by clicking on the page numbers (e.g., 1, 2) to make an API call with a limit of 10 and an offset incrementing from 10 after the initial call. https://i.stack.imgur. ...

What happens when the user closes a notification in GetUIkIt 3?

Is there a way to detect when a UIKit notification has been closed? The UIkit notification plugin () mentions that it has a close event. Can this be utilized for notifications triggered programmatically as shown below? e.g. UIkit.notification({ mess ...

I am looking for a pair of HTML tags that will allow one to be visible while the other is hidden, and then switch when needed

I am in the process of constructing a webpage dedicated to learning Japanese vocabulary. Each word will be presented within a sentence, just like the example shown below. 森林に散歩する take a walk in the forest In order to facilitate the practic ...

Using single-line ellipsis with the Material-UI select and option components

I have a TableHead Component with multiple columns. Among them is a <Select> element that allows users to filter the table content based on one of four statuses. I am trying to implement an ellipsis at the end of the option string if it exceeds its c ...

AngularJS radio buttons are interactive HTML elements that allow users

I've been looking for a solution to bind my radio inputs to a model in a simple and clean way, but I haven't found one that works for me yet. In my HTML, I have: <input ng-model="searchByRma" type="radio" name="search-type"> <input ng-m ...

Which event listener in the DOM API should I use to trigger an action when a form field is focused, whether through a mouse click or by tabbing?

My aim is to increase the size of form fields when the cursor focuses on them, either through a mouse click or by tabbing using the keyboard. By utilizing document.activeElement focus, I can identify when the user clicks inside a text input or selects an ...