Updating CSS style across different pages using JavaScript

I am working with two different pages: index.php and app.php

On the first page (index.php), I have elements that need a change in CSS style, such as font size. The specific element on this page is .options-parameters-input.

The second page (app.php) contains my options panel with input fields where I can specify the font size for the desired element. In this case, the input field is a textarea with ID #heretype

Here is a snippet of the code:

<div id="one">
<div class="options-parameters-input">
This is testing
</div>
</div>
<br /><br /><br /><br /><br /><br /><br />
<table width="750" border="1" cellspacing="3" style="float:left;">
  <tr>
    <td>Type font size</td>
    <td><textarea id="heretype"></textarea></td>
  </tr>
</table>


$("#heretype").on("keyup",both);

function both(){
$(".options-parameters-input").css("fontSize", this.value + "px");
}

You can view a jsfiddle example here: http://jsfiddle.net/gxTuG/106/

I am facing an issue in transferring the new CSS style value from one page (app.php) to another (index.php). This is because the inputs are on app.php while the elements requiring styling changes are on index.php.

If you have any suggestions or solutions, I would greatly appreciate your help.

Thank you!

Answer №1

There are two main methods to achieve this:

On the server side: Save the value in a database for permanent storage. Then on the next page, apply it using CSS.

On the client side: Store the value in a query parameter, cookie, or local storage for temporary storage. Then on the next page, apply it using CSS.

Answer №2

While it may not be possible to directly achieve this as mentioned by Sarah Johnson, there are alternative methods that can be used. For example, you can utilize PHP to store the changes either in a session or database. This way, when the user revisits index.php, a unique <style>...</style> will be dynamically generated based on their saved preferences. Saving the setting can be done through a form submission or an ajax call, which is slightly more complex.

Another option is to manipulate cookies using JavaScript to save data and retrieve it on a different page.

Answer №3

Updating the CSS of one page using JavaScript code from another page is not possible. However, you can establish a shared identifier between the pages such as using a URL param like index.php?fontsize=10 and app.php?fontsize=10.

You can retrieve the value of the parameter fontsize in your code on both pages and set it to the desired value, passing it around with JavaScript.

You can either set a default value for the font size at the start of the app or check if the fontsize param exists in your JavaScript code.

Refer to this answer for more information.

Another option is utilizing localStorage -> Window.localStorage() or cookies -> Document.cookie().

If you prefer not to use server-side code to store the fontsize value in a database, I recommend either using the URL param method or setting a localStorage object like fontSize and updating it as needed.

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

Image expanded on a spherical surface

How can I resolve the issue of my image being stretched out and pixelated when using three.js to cover a sphere with a photo? Here's the code snippet causing the problem: moonSurface = new THREE.Mesh( new THREE.SphereGeometry(moonRadius -.1, 50, ...

"The height of the text on the y-axis in jqplot was causing clipping

Within the y-axis, I used text for 2 lines with "/n" but my text is getting clipped. How can I expand the text line height? axesDefaults: { tickRenderer: $.jqplot.CanvasAxisTickRenderer, tickOptions: { angle: 0, ...

What is the most efficient method for transmitting 30 variables from the client side to the server?

Hello, I am currently working on a project where users are able to create and modify about 30 variables. These variables do not come from user input, so submitting a form is not feasible. After the modification process is complete, a JavaScript function pr ...

Require checkboxes in AngularJS forms

Currently, I have a form that requires users to provide answers by selecting checkboxes. There are multiple checkboxes available, and AngularJS is being utilized for validation purposes. One essential validation rule is to ensure that all required fields a ...

Locating the exact position of a DOM node within the source document

Purpose In the process of creating a series of 'extractor' functions to identify components on a page using jsdom and nodejs, I aim to organize these identified 'component' objects based on their original placement within the page. Ch ...

The fonts in node.js are not functioning as expected, without displaying any error messages

I'm having trouble implementing custom fonts on my website. Despite following the correct file structure, the fonts do not seem to be loading. My project files are organized in the following manner: https://gyazo.com/5ee766f030290e5b2fa42320cc39f10b ...

Creating printable reports using Jspdf with data from SlickGrid

Has anyone successfully used JsPdf with a slickgrid on a webpage? I am currently using their HTML renderer, but I know it's still in early stages. I have added the HTML2Canvas cdn, but I'm unsure how to implement it. Here is my Fiddle $(docum ...

Changing background image of a container in Vue with dynamic functionality

How can I apply a background image to a div using Vue.js? I attempted the following method, but it is not working. So far, this is what I have tried: https://i.sstatic.net/wLD7N.png Currently, the display on my webpage looks like this: https://i.sstatic. ...

The placement of term.js is always at the bottom of the body

Seeking help with JavaScript as a beginner, I am facing issues with placing an element in my project. Although I can easily open terminals and write to them, every time I create one, it gets appended at the end of the body. I referred to this example for ...

Calculating the mean value of an array containing objects with a single key-value pair in JavaScript

I have a list of users along with their ages, and I need to calculate the average age of all users. I initially attempted to use the reduce method for this task, but encountered syntax errors preventing successful implementation. Below is the code snippet ...

Module 'js' not found

Upon adding a request, I encountered this error message. I attempted npm install js and added var js = require("js") in my app.js file, but unfortunately it did not resolve the issue. My express server is running on localhost. Error: Cannot find module &a ...

Unusual Behavior of CSS and JQuery Selectors

As I was working with jquery selectors, I encountered something quite unexpected. This is an example of my HTML: <ul id="unordered-list"> <li> <img src="https://www.google.com/images/srpr/logo4w.png" width="40" height="40"/> ...

Using JavaScript to set a form via a parameter in the URL

Is there a way to dynamically change the SetForm based on a parameter in the URL, such as radio.php?land=form2? I tried doing it this way but it doesn't seem to work. However, when I do it manually via the menu, it works. This is my first time attemp ...

What steps should I take to make my Twitter widget adaptable to varying screen heights?

I'm currently utilizing react-twitter-widgets to incorporate a Twitter timeline within my application. While everything is rendering correctly, the height of the timeline extends down the entire page. Is there a way to adjust the widget's height ...

Is it feasible to combine border-radius with a gradient border-image?

I'm trying to style an input field with a rounded border using the border-radius property, and also add a gradient to that border. Despite successfully creating both the gradient and rounded corners separately, I am unable to make them work together. ...

The animation feature of MDBootstrap Angular Material does not seem to be functioning as intended when applied to

Trying to implement the "Inputs with background animated border - Material 2.0" feature from this link. Also utilizing the "Lazy Loading" approach for routing. Original Design https://i.sstatic.net/O1LwO.png Expected Functionality https://i.sstatic.ne ...

Querying arrays in Mongoose

I'm facing a challenge with querying my database to extract a specific array element from within a user model. The schema is structured as follows: const schemaUser = new mongoose.Schema({ username: { type: String, required: true, unique: true }, ...

In NextJS with SASS modules, the selector ":root" is considered impure since pure selectors are required to include at least one local class or id within them

Lately, I made the switch to using modules in my next.js project. However, I keep encountering an error in my newly created .module.scss files that says: "Selector ":root" is not pure (pure selectors must contain at least one local class or id)". It seems ...

Create a line connecting two divs using jQuery's DOM manipulation capabilities

Looking to connect two divs with a straight line, I stumbled upon jQuery DOM line, which appears to offer a more streamlined solution compared to jsPlump. I attempted to incorporate it into my code, but unfortunately, it's not working as expected. Be ...

What could be preventing this code from automatically scrolling to the designated class when a key is pressed?

When a key is pressed on the document, the default action is prevented and the element with the ID "show-all-win" is animated to scroll to the position of the element with the class "key_" plus the key code of the pressed key. This animat ...