Inserting a page separator using JavaScript

I'm attempting to incorporate a page breaker at the conclusion of every HTML section, and here's my current code snippet:

printHtml += '<div id="#pageBraker">end of current page</div>';
$("#pageBraker").css("display","none");
$("#pageBraker").css("display","block");
$("#pageBraker").css("page-break-before","always");

Upon inspecting each part, I see end of current page without encountering any console errors. My queries are as follows:

1. How can I apply @media to my pageBraker or within my HTML?

2. Is this the correct approach for dynamic insertion?

Below is my complete code snippet:

//elemsToPrint - list of elements I want to print but with page breaker between
for (var index = 0; index < elemsToPrint.length; index++) {
    printElement = elemsToPrint[index];
    printHtml += $(content).parent().html();
    printHtml += '<div id="#pageBraker">end of current page</div>';
    $("#pageBraker").css("display","none");
    $("#pageBraker").css("display","block");
    $("#pageBraker").css("page-break-before","always");
    //send to printer
    _printElement(printHtml);
}

The main objective is to have each part (printHtml) printed on a separate page.

I appreciate any assistance provided.

Answer №1

Avoid adding styling directly into your for-loop. Instead, use a stylesheet like this:

#pageBraker {
  display: none;
}

@media print {
  #pageBraker {
    display: block; 
    page-break-before: always;
  }
}

In this example, the @print media query is applied for specific styling in print view only. The element #pageBraker will remain hidden outside of print view.

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

Refresh the webpage when using the browser's back or forward button in AngularJS with ui-router

In my AngularJS app, I have configured the ui-router state as follows: $locationProvider.html5Mode(true); $stateProvider .state('index', { url: '/index?zoom&center', views: { ...

What makes @font-face function on Safari but not on Firefox in Mac versions?

Can anyone help me troubleshoot a font issue on my website? I've added the following code to the CSS file, and it's working fine in Safari, but not in Firefox. I'm new to coding, so please bear with me if this seems like a basic question. I& ...

Issue with Live Sass Compiler in Visual Studio Code not creating .css file in designated css folder

In an attempt to organize my project better, I wanted to set the save location of my CSS file to its own folder within the Live Sass Compile Config (Live Sass Compile > Settings:Formats). Initially, I set the savePath to "/css". However, when saving my ...

PHP is unable to retrieve the formData object sent via ajax

I've been experimenting with sending a formData object to PHP (I'm following a tutorial at this link), but I'm encountering some obstacles. Firstly, I create the formData object and fill it with: var formdata = new FormData(); formdata.appe ...

Personalize Dropdown Menu, determining when to initiate the hide menu action

When creating a dropdown using ul and li tags, I am faced with the dilemma of determining the ideal time to hide the dropdown menu. The dropdown menu is designed to display values based on user input as they type, updating dynamically. Initially, I had se ...

Issue: The module 'vue-loader-v16/package.json' cannot be located

Just transitioning from React.js to Vue.js and encountering an issue with the Vue loader that didn't occur initially. However, starting the server crashes the app from the second time onwards. npm run serve > <a href="/cdn-cgi/l/email-protecti ...

Strategies for updating arrays in Redux state

I am currently working on developing a project similar to "Trello" using react, redux, nodejs, and mongoDB. However, I have encountered an issue where when I add a card to a list, the redux state is not updated. As a result, I can only see the newly added ...

Retrieve a data value from a JSON object by checking if the ID exists within a nested object

I am facing the challenge of navigating through a JSON object where child objects contain IDs that need to be matched with parent properties. The JSON structure looks like this: { id: 1, map: "test", parameter_definitions: [{ID: 1, pa ...

Save the data in Redux and access it back

I am currently using material-ui-dropzone to upload an array of files and store them in redux However, when I try to retrieve the file from the redux state, it is not recognized as type "File" and appears differently in devtools https://i.stack.imgur.com ...

Posts that are repeated when making an AJAX call in WordPress

I have been experimenting with implementing a 'load more' button feature in my WordPress site. The concept is simple - users click the button, and additional posts are loaded using AJAX without the need for page reload or pagination. Following a ...

When populating data, two ID fields (_id and id) are generated as duplicates

Upon retrieving information from my database, I have noticed the presence of an additional id field alongside the standard _id field. These two fields consistently contain identical values. However, it seems that the id field appears only during population ...

Stubbornly Persistent Server Error (Django and Ajax)

I've been using Ajax to send my data over to views.py, but despite terminating all the urls and configuring the form action for my url, I'm still encountering an internal server error. I'm struggling to pinpoint the issue, so any assistance ...

What is the reason behind the cross-origin error thrown by JSON.parse?

When I don't use JSON.parse, my code works perfectly fine. However, as soon as I attempt to parse or stringify my data object, a cross-origin error is thrown. Why is this occurring and what steps can I take to resolve it? The snippet of code in Title ...

The function or method 'uploadify' is not compatible with this object

I am encountering an issue while attempting to utilize the jQuery uploadify functions within my aspx page. Upon the page load and execution of $(document).ready, an exception is triggered. It appears that, for some unknown reason, the FileUpload1 control i ...

How to vertically center text with button label in Bootstrap 4

I have a simple objective: I want to align a line of text to the left and a button to the right, with the text vertically aligned to the button's label. I've tried adjusting padding and margins without success. I feel like there must be an easy s ...

What is the best way to arrange an array of objects based on a specific condition

I have an array of objects that contain properties. My goal is to sort them by status in a specific order: 15, 17, and then 16 using JavaScript. In this array of objects, any object with a status value of 16 should be placed at the end, while the rest s ...

Encountering an error with my electron application built using create-react-app

While I'm working on my project, my electron window is showing this error message. TypeError: fs.existsSync is not a function getElectronPath ../node_modules/electron/index.js:7 4 | var pathFile = path.join(__dirname, 'path.txt') 5 | ...

What could be the reason for the change event not triggering when using selector.val(1234)?

Similar Question: Why doesn't jQuery textbox.val('xxxx') trigger change event? I am facing an issue with a plugin that should execute when the value of a specific "selector" is updated. While it functions correctly during standard user ...

Tips for preventing interruptions in periodic HTTP requests using jQuery?

As a newcomer to jQuery, my goal is to leverage it for fetching data from a server every 3 seconds. This server transmits information in JSON format at the same interval, with the crucial numerical array field { "samples" : [10,15,-7,19,34,...] }. Here is ...

Eliminate the gap between the header and the content of the page

I'm trying to fix the issue with my header having extra space above it. I want it to be flush against the top of the page. Attached is an image showing the problem. https://i.sstatic.net/nIFaw.jpg Take a look at my code below: body { background ...