What is the process for adding CSS to a function in web development?

Is there a way to apply CSS similar to the image below to a one-page QR ticket generated by Java? I've tried altering the current CSS without much success. desired output

This is the existing code for the function:

function openQRCodeWindow(registrationId) {
  var qrCodeImage = generatedQRCode._el.firstChild.toDataURL("image/png");
  var firstName = document.getElementById("first-name").value;
  var lastName = document.getElementById("last-name").value;
  var email = document.getElementById("email").value;
  var contactNumber = document.getElementById("contact-number").value;
  var seatNumber = document.getElementById("seat-number").textContent;

  var newWindow = window.open();
  newWindow.document.write('<style>');
  newWindow.document.write('body { display: flex; flex-direction: column; align-items: center; background-image: url("megapolis background.png"); background-size: cover; }');
  newWindow.document.write('.container { display: flex; justify-content: space-between; align-items: center; padding: 50px; border: 2px solid #ccc; border-radius: 10px; width: 400px; }');
  newWindow.document.write('.qr-container { margin-right: 20px; }');
  newWindow.document.write('.details-container { text-align: right; }');
  newWindow.document.write('.button-container { margin-top: 20px; text-align: center; }');
  newWindow.document.write('.button-container button { margin: 0 5px; }');
  newWindow.document.write('</style>');

  newWindow.document.write('<h2>QR Code</h2>'); // Place the QR code title here
  newWindow.document.write('<div class="container" id="form-container">');
  newWindow.document.write('<div class="qr-container">');
  newWindow.document.write('<img src="' + qrCodeImage + '">');
  newWindow.document.write('</div>');
  newWindow.document.write('<div class="details-container">');
  newWindow.document.write('<h3>Details</h3>');
  newWindow.document.write('<p>Registration ID: ' + registrationId + '</p>'); // Display the Firebase-generated ID
  newWindow.document.write('<p>First Name: ' + firstName + '</p>');
  newWindow.document.write('<p>Last Name: ' + lastName + '</p>');
  newWindow.document.write('<p>Email: ' + email + '</p>');
  newWindow.document.write('<p>Contact Number: ' + contactNumber + '</p>');
  newWindow.document.write('<p>' + seatNumber + '</p>');
  newWindow.document.write('</div>');
  newWindow.document.write('</div>');
  newWindow.document.write('<div class="button-container">');
  newWindow.document.write('<button id="save-image-btn">Save Image</button>');
  newWindow.document.write('<button id="email-btn">Email</button>');
  newWindow.document.write('</div>');
  newWindow.document.close();

I attempted to use inline styles in the JavaScript function, but it didn't produce the desired results. I'm unsure of how to properly add CSS to JavaScript functions.

Answer №1

Avoid using the document.write() method as it is not recommended.Visit this link for more information

Instead of document.write(), consider using the appendChild method to add elements to qr-container. Styling can be done in the style.css file and then use classList.add to add a class name to an element.

Your code seems lengthy, so here's a simpler example:

function openQRCodeWindow(registrationId) {
const container = document.querySelector('.qr-container');
const titleElement = document.createElement('h1');
titleElement.textContent = 'ABC';
titleElement.classList.add('title');
container.appendChild(titleElement);
}

openQRCodeWindow('')
.title {
  color: red
}
<div class='qr-container'></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

Show an error message in-line with a line break

I'm struggling to show inline error messages separately. What I want to achieve: Error 1 Error 2 However, they are currently displaying on the same line like this: Error 1 Error 2 I attempted to use document.createElement("br") without success. ...

The dimensions of a div are constantly fluctuating

I need to adjust the width of .center. There are multiple rows, some with a button and others without. Since it loads dynamically, the width of .center should vary for each row. For instance, in the first row, .center should have a width of centerWidth2 ...

Have you considered utilizing "call for('express')()" instead?

When creating a simple Web server with NodeJS and Express, most tutorials provide examples like the following: const express = require('express'); const app = express(); app.listen(3000, () => console.log("Started")) app.get(' ...

Using JavaScript to modify the -webkit-animation-play-state

Hello, I'm currently facing a challenge in changing my css3 -webkit-animation-play-state property from paused to running upon clicking on another div. Does anyone have any suggestions on how I can achieve this? I believe JavaScript might be the way to ...

My AngularJS datetimepicker is not showing the calendar

Looking to add a dropdown datepicker to my webpage. This is what I have so far: <code>http://plnkr.co/edit/UaD8A06ilA6K6MsDL95s?p=preview</code> Having trouble getting the calendar menu to display. ...

The console is displaying a null value outside of the block, however, the correct value is returned when

I am having an issue where the first console.log() is not returning the correct value, but the second one is. Can anyone provide assistance with this problem? angular.module('resultsApp', ['ngCookies']) .config(['$qProvider&a ...

Issue with Ajax request not functioning properly upon reloading the page

My home page is designed in PHP and includes checkboxes for the brand and store list. At the end, there's a submit button provided below. <html> <head> <title>Insert title here</title> <script src="http://ajax.googleapis.co ...

Exploring the power of JavaScript template literals within the Document Object Model

I am curious as to why this particular template literal is not functioning correctly when I try to implement it using JavaScript DOM for styling CSS. Any assistance in helping me understand this issue would be greatly appreciated! let weatherColors = [& ...

Attempting to prevent lateral scrolling

I have created a div with an initial position set. <div class="guide" > <style> .guide{ height:150px; width:200px; position:absolute; right:-170px; top: 10%; } </style> My goal is for the div to slide out from t ...

A step-by-step guide on transferring Data URI from canvas to Google Sheet using the fetch method

I am trying to send an image as base64 code to a Google sheet using fetch. However, I am encountering an error that says "data_sent is not defined" when I run my code. I need help identifying the problem and finding a solution to fix it. For reference, & ...

Using Vue.js code on an HTML file is only possible when the necessary CDN is included

Just diving into Vue.js and I've got a header html that doesn't include the cdn link for Vue.js. <nav class="navbar navbar-toggleable-md navbar-inverse"> <div class="collapse navbar-collapse" id="navbarSupportedContent"> ...

What's the reason behind beforeunload not triggering?

I've gone through numerous similar posts, but I'm struggling to figure out what's not quite right with this code snippet: $(window).on("beforeunload", function () { debugger handleBeforeUnload(); return false; }); function handl ...

The show.bs.modal event has been triggered when the .show() method is used on elements within the

Recently, I discovered that the event show.bs.modal is triggered not only when the modal itself is shown, but also every time you call the .show() method for an element within the modal. To attach the event handler, you would typically use the following c ...

"Utilize Azure Storage to easily upload an image file and retrieve its URL for seamless display using the img

I am looking to utilize Azure file storage for storing image files. I have the ability to upload an image file using the code snippet below: ... const uploadBlobResponse = await blockBlobClient.uploadFile('./test.png'); console.log('Blob was ...

Disrupted design by bidirectional text manipulation

Within the header of my website, there is a section intended to greet the user with "Welcome username." <span>Welcome <?php echo $username; ?>.</span> The issue arises when a user changes their name to include the U+202E character, whic ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

How can I showcase the selected file using jQuery's querySelector?

I have successfully implemented a file upload feature using jQuery querySelector. Now, I am looking for a way to display the selected file name in a span element after the user selects the file. Here is my HTML code: <span id="filename"></span&g ...

Unable to generate Google Charts in PDF using Wkhtmltopdf

I am facing an issue while trying to create multiple charts in a PDF using Google Charts. I am utilizing CakePDF with the Wkhtmltopdf engine for generating the PDFs. The problem arises when attempting to load the Google Chart code into the PDF file. Below ...

Undefined Variable Errors

I am attempting to pass the errors variable (in this scenario, it is a fix) from the server to the client so that it can display the errors using the ejs template engine. The errors are listed in the variable through push and I then retrieve them in error ...

What is the best way to display cards next to each other on desktop and mobile devices while maximizing available space?

In the world of React components, there exists a card representation within a grid view that adapts based on available screen space; function EngineerCard(engineer: EngineerDetail) { return ( <div className='engineerCard'> <d ...