Update the value of a variable in SCSS with a click event

I have been working on developing online courses using a template created by another developer in SCSS and vue.js. The color scheme of the template is based on the SCSS variables $primary and $secondary. I am considering adding a color-blind option that would allow users to switch between complementary colors for Normal, Deuteranopia, and Tritanopia spectrums.

This is still in the design prototype phase, but my idea is to have circular color options in a dropdown menu that can change the primary color of the entire page when clicked.

https://i.sstatic.net/1ut0h.png

Since I am not very familiar with vue and scss, I am uncertain about how to implement this. My plan is to update the $primary: ""; variable based on the user's selection. For example:

 $(".changePrimaryColor").click(function(){
     $primary == $theme-blue;
 });

I am also considering incrementing the value of $primary by 1 or 2 depending on the user's choice, but I am unsure about the specifics.

Any suggestions or guidance would be greatly appreciated! Thank you!

// Theme colors
$theme-green: #c4d600;
$theme-blue: #1a397c;
$theme-red:#9b1717;

$primary: $theme-green;
$secondary: $theme-red;
$success: #288515;
$danger: #a61616;
$warning: #efa92c;

$theme-colors: (
    "primary": $primary,
    "secondary": $secondary,
    "success": $danger,
    "danger": $success,
    "warning": $warning,
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/sass.js/0.9.2/sass.min.js"></script>

Answer №1

Regrettably, scss is unable to achieve this functionality (although it would be truly amazing). The reason for this limitation is that scss compiles into standard css code. Therefore, your scss variables cease to exist once the page is loaded by the client.

However, you can take advantage of css properties/variables instead, as they do allow for rendering! https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties

Essentially, tasks like changing colors become quite straightforward!

const button = document.querySelector('button')

function changeColor() {
  document.body.style.setProperty("--bg-color", 'DodgerBlue');
}

button.addEventListener('click', changeColor)
:root {
  --bg-color: tomato;
}

body {
  background: var(--bg-color);
}
<button>Change color</button>

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

Harnessing the power of Bootstrap 5 alongside VueJS 3

I have been delving into the world of Vue.js and Bootstrap lately. I recently discovered that you can integrate Bootstrap with Vue.js, so I decided to give it a shot. My code is fairly straightforward - just a dropdown and a popover. While the dropdown is ...

execute regular javascript with angualrjs

Seeking to integrate legacy javascript code for affiliate tracking into an angularjs app <script type="text/javascript"> //<![CDATA[ /*** Do not change ***/ var AWIN = {}; AWIN.Tracking = {}; AWIN.Trackin ...

Transfer the index of a for loop to another function

Can you explain how to pass the 'i' value of a for loop to a different function? I want to create a click function that changes the left position of a <ul> element. Each click should use values stored in an array based on their index posi ...

It is not possible to utilize Vue.js to employ the <slot> feature for rendering the parent component

After attempting to implement the slot feature following the example on the Vue official site, I encountered a failure despite keeping my code very concise. Parent Component <template> <subMenuTemp> <div class="text" > paren ...

Sending calendar events through ajax response in jQuery FullCalendar can be accomplished by following these steps:

I'm currently using jQuery Fullcalendar in conjunction with Codeigniter. Initially, I have a table that displays schedule times and corresponding names. When I load the calendar page, I send data to fullcalendar.js directly, but I would prefer to rece ...

The JSON.parse function encounters issues when trying to parse due to a SyntaxError: Unexpected character found after JSON at position 2, causing it to be unable

I've encountered an issue with my JavaScript code when trying to retrieve the value of the details field from JSON data. While all other values are successfully passed to their respective fields, the details field generates the following error: "Unabl ...

Shader error in Three.js occurring on various computers with the same browser

For the last 3 months, I have been immersed in developing with Three.js, successfully using it across various browsers and computers. However, a week ago, an error started popping up on my home computer whenever I attempted to load my website: three.js:29 ...

Refreshing a Particular JavaScript File in Electron Upon Request

Lately, I've been working on an Electron app that involves storing data in a javascript file. The data needs to be decrypted when the user logs in and displayed, then encrypted again when the user logs out. While logged in, users can add new data to t ...

Can you explain the functionality of this code in terms of conditional statements without the use of "if" or "else if" statements?

Upon encountering a nested logic problem that required solving, I stumbled upon an unconventional solution online which caught my eye. The code structure seemed unfamiliar to me and it intrigued me how the use of "if" and "else if" statements was differe ...

Utilizing jQuery's map function to extract data attributes from lists and store them in an array

View the working example Check out the failed example I'm currently utilizing a pie chart plugin available on GitHub. My objective is to generate an array resembling the structure below: [["Male",12,"some text","#222"], ["Fe ...

I'm getting a "module not found" error - what's the solution?

const { getIo } = require('services/socketio'); const restful = require('utils/restful'); const publicApiService = require('services/publicApi'); const accessTokenMiddleware = require('middleware/accessToken'); const ...

Troubleshooting issues with AngularJS's minDate functionality

I have been trying to set the minDate for the datepicker to today, following the example on the angularJS bootstrap site. However, it seems like something is not working correctly. There are no console errors showing up, but it appears that the minDate is ...

Having trouble retrieving the table value from an HTML document?

I am trying to retrieve specific information from this source: https://i.sstatic.net/g1wDj.png This information is crucial for fetching data from a database using a primary key. However, extracting this value has proven to be quite challenging. Upon docu ...

Dealing with Laravel and AJAX - Issue with Loading DIV

I find myself in a perplexing situation. Despite not encountering any errors in my apache logs or browser (chrome), I am faced with an issue that has left me baffled. On a specific page (localhost/admin/networks), I can click on an item from a database-ge ...

Obtaining an array of weeks for the previous two months from the current date using JavaScript

Looking to generate a list of weeks for the past two months using JavaScript. Any suggestions on how to accomplish this task would be greatly appreciated. [ { "week_start": "8/01/2016", "week_end": "8/06/2016" }, { "w ...

Adjusting the styling of a webpage in real-time

Here is the CSS code I am working with: .myTabs .JustForFun .ajax__tab_inner:hover { width: 109px; background: url ("/images/csc_tr.png") no-repeat 100% 0%; margin-right: 2px; background-color: #BBC614; } I want to change the background-c ...

How can recursion be implemented when the items are interdependent?

I am looking to create a function that can generate a list of individuals upon whom a specific person relies. The complexity here lies in the fact that these individuals may, in turn, rely on the original person. To illustrate: const people = { ' ...

An unexpected error occurred: ReferenceError - document is undefined

Error Alert: Unhandled Runtime Error ReferenceError: doc is not defined Source of Issue components\Modal.js (42:18) @ updateDoc 40 | await uploadString(imageRef, selectedFile, "data_url").then(async (snapshot) => { 41 | const do ...

I am experiencing difficulties with my focusin not functioning properly for the input element that I added

I am working on a program that includes a dynamic table with the following structure: <table id="selectedItems"> </table> I plan to use jQuery to append elements to it like so: var i=0; $("#selectedItems").html(''); $("#sel ...

Enhancing middleware chaining in Express

Below is the code for my Express configuration: var server = express() .use(express.cookieParser()) .use(express.session({secret: buffer.toString('hex')})) .use(express.bodyParser()) .use(express.static('./../')); serv ...