Programmatically changing the stylesheet does not seem to be working to apply the

I'm currently working on a feature that allows the User to customize the application's style.

Index.html

<!DOCTYPE html>
<html>
    <head>
        <link id="style" rel="stylesheet" type="text/css" href="red-stylesheet.css" />
    </head>
</html>

Javascript

function changeStyle(sheet) {
    document.getElementById('style').setAttribute('href', sheet);
}

Despite my best efforts, I'm facing an issue with changing styles. By default, the theme is set to red, but when I attempt to switch to blue, all styling disappears and I can't revert back to the red theme.

What steps should I take next?

(PhoneJS/DevExtreme application)

Initial App Appearance

https://i.sstatic.net/hgDSH.png

After Selecting "Blue" Option

https://i.sstatic.net/lms7i.png

Answer №1

Here is a helpful tip:

$('head>link[type="text/css"]').attr('href',newSheet);

Answer №2

-----------Give This a Shot -----
Check out this code snippet to see it in action

<!DOCTYPE html>
<html>
<head>
<link id="pagestyle" rel="stylesheet" type="text/css" href="default.css">
<script>
function switchStyleSheet(sheet){
    document.getElementById('pagestyle').setAttribute('href', sheet);
}
</script>
</head>
<body>
<h3>Changing Stylesheets Dynamically with JavaScript</h3>
<button onclick="switchStyleSheet('dark.css')">Dark Theme</button>
<button onclick="switchStyleSheet('default.css')">Default Theme</button>
</body>
</html>

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

Experiencing problems with malfunctioning javascript tabs

As a beginner user and coder, I'm currently working on creating a portfolio website. I had the idea to implement tabs that would allow for content swapping, but unfortunately, I'm having trouble getting the JavaScript to function correctly. I at ...

Make sure to close any existing Featherlight windows before trying to open another one

I'm setting up multiple featherlight instances when the page loads jQuery('.feedback').featherlight(jQuery( "#feedback-box" ), { closeIcon: 'close'}); jQuery('#imprint').featherlight(jQuery( "#imprint-box" ), { closeIcon ...

An error occurred with Express and Passport: ['ERR_HTTP_HEADERS_SENT']

Currently, I am diving into an ebook tutorial and have encountered a roadblock in a particular piece of code. The code is designed to take a username and password in JSON format through Insomnia or Postman, and it should return a login success cookie. Howe ...

Enhancing TypeScript Types with a custom method within an Angular 2 context

I am working on an Angular 2 project using the CLI. Currently, I am trying to add a custom method to the String type as shown below. interface String { customMethod(): number; } String.prototype.customMethod = function() { return 0; } Despite my ...

The absence of AudioPlayer may be responsible for the compilation failure on Vercel

Here is the latest code snippet import { useState, useEffect, useRef } from "react"; import { FaPlay, FaPause, FaForward, FaBackward } from "react-icons/fa"; export default function Player() { const [isPlaying, setIsPlaying] = useState(false); const [ ...

Exploring package.json: Uncovering the Installed Components of a Module

I am working on a project with the following structure: __tests__ example src .bablerc .eslintignore .eslintrd .gitignore package.json package-lock.json README.md The package.json file has the following parameters: { "name": "", "version": "0.0.1", ...

Ways to refresh a div element without causing it to blink

I'm new to web development and I have an HTML webpage that retrieves data from a database and displays information based on the retrieved data. I would like to update the data every second without causing the webpage to blink. Can you please modify th ...

Arrange the div next to each other

Looking to position two divs side by side with widths totaling 100%? The left div will have a specified width in pixels that can change, while the right div should expand to fill the remaining space. Having trouble with this layout? Check out my example o ...

Ways to adjust the location of the scrollbar using CSS

Can the position of the scroll bar be modified using CSS to switch it from left to right or from bottom to top? ...

When jQuery updates the content, the div content disappears temporarily

I am currently using jQuery to generate HTML by fetching data from a JSON file. Below is the code snippet where I create the HTML: $(document).ready(function () { $('#searchForm').submit(function () { var entry= $("#searchFo ...

Is there a way to modify initialValue's value following every iteration?

I need assistance in changing the value of initialValue after each operation. For example, if I input 1000, the output should be 11,000 (10,000 + 1,000). Then, if I subtract and input 2000, the output should be 9,000 (11,000 - 2,000). Can anyone provide gu ...

Manipulate JSON objects in AngularJS by deleting or adding items

This JSON is currently in my possession: var videos = [ {"id":"9854", "time": 56}, {"id":"7859", "time": 29}, {"id":"8745", "time": 59} ]; If an item is missing from the JSON, I want to add it, but if it's already there, I want to remove ...

Form Validation Using a Separate Function

Currently, I am utilizing the jQuery Validation plugin from http://jqueryvalidation.org/ to validate my forms. By default, the validation process is triggered when the form is submitted. However, I would like to initiate the validation when a specific func ...

What could be causing the slow compilation of my Next.js pages within the app directory, and what steps can be taken to improve the speed of this

Currently, I am working on a Next.js project that uses the 'app' directory structure. However, during local development, I have been facing significant delays in compile times. Here's a breakdown of the compile times I am encountering: - Th ...

The challenges of updating AngularJS partial templates and handling refresh in 2-way binding

I've encountered an issue with a partial template that's loaded outside of my ng-view, complete with its own controller. Here's a breakdown. Basic template layout <html ng-app="myApp"> ... <div ng-include src="'myPartia ...

ISO 8 takes a different approach by disregarding the meta viewport tag and autonomously adjusts the website layout to

<meta name="viewport" content="width=device-width,initial-scale=0"> I am facing an issue with my code not running on iPhone 6 and causing a problem with the responsiveness of my site. Any suggestions on what steps I should take? The site is constru ...

displaying the identical image from various perspectives

I have an arrow image that I need to display in different angles - top, left, bottom, right. I was considering what would be the best approach: 1: Changing the image direction via CSS and loading the main image <img src="a.png"> <img src="a.png" ...

"Learn the process of extracting information from a database and exporting it to a CSV file with Angular 2

Currently, I am facing an issue where I need to retrieve data from a database and export it to a CSV file. While I am able to fetch all the data successfully, I am encountering difficulty when trying to fetch data that is in object format as it shows up as ...

Eliminate items from a list that have duplicate properties

I have a collection of objects, each with a unique NAME property. However, there are duplicates in the collection where some objects share the same NAME. const arr = [ {name: "x", place: "a", age: "13" }, {name: "x", place: "b", age: "14" }, { ...

Is it possible to use the Stop Button on the HTML5 Audio Tag to halt a live MP3 stream

Is there a way to add a stop button as well? Currently, I have play and pause buttons, but the stop function doesn't truly clear the music buffer in the browser; it just stops playback without resetting to the beginning. This is fine for MP3 files but ...