Modify the value of the attribute "class" for a particular element

I am currently using the cur_font class in my CSS stylesheet. I would like to switch the font-family to 'arial' using jQuery and have this new font apply to the html, body, h2, h1, div, and td selectors.


    <style>
    .cur_font html,body,h2,h1,div,td { font-family:'tahoma'; }
    </style>

    <script>
        $(".change_font").on("click", function (e) {
            var font='arial';
            $("style.cur_font").css("font-family",font);
        });
    </script>

I am attempting to update the font-family property for the cur_font class using jQuery

Answer №1

To easily accomplish this task, one method is to toggle a class on the body element and apply different styles accordingly.

Another approach involves looping through document.styleSheets, then iterating over the cssRules to check for matching selectorText. If there's a match, the rule can be modified.

An alternative option is to utilize a CSS variable and update the variable value dynamically.

document.querySelector(".buttons").addEventListener("click", evt => {
  const btn = evt.target.closest("button");
  if (!btn) return;

  const color = btn.dataset.color;
  document.documentElement.style.setProperty('--myVariable', color);
});
:root {
  --myVariable: red;
}

div {
  background-color: var(--myVariable);
}
<div>Hello World</div>

<div class="buttons">
  <button data-color="blue">blue</button>
  <button data-color="red">red</button>
  <button data-color="yellow">yellow</button>
</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

Update the fuelux treeview after uploading a file

Currently, I am utilizing the jquery fileupload plugin to facilitate the process of uploading a file and then using the data from said file to populate a fuelux treeview. I have configured an ajax call for handling the file data where the information is fe ...

What is the best way to change an image when hovering over it?

Despite my efforts, the image did not change to another one when hovering. Instead, the second image appeared alongside the first one. Here is the code I used, can you please point out what I might have done wrong? .card { width: 130px; height: 195p ...

Dilemma with Buttons

I'm encountering an issue with my top navigation buttons. When I resize the window horizontally, the buttons behave in a certain way. If the browser window becomes too small horizontally, I would like them to act differently. Is there a way to achie ...

Place the button and text box side by side

I am attempting to align the following button and text box side by side, with the button on the left and the text box on the right. I have tried using span and margin properties, but currently the text box is displaying below the button. Can you suggest ...

Is it possible to use Primefaces tooltip to show at the bottom of the page

When I hover over the PrimeFaces tooltip, it displays at the bottom of the page. Here is my code and image link: <!DOCTYPE html> <h:html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com ...

Steps for generating an HTML button when an Angular function evaluates to true

I am working on a table in HTML that uses ng-repeat to display data. Here is the code snippet for this table: <tbody> <tr dir-paginate="x in serverData | filter:filterData | filter:filterData | itemsPerPage: itemPerPageValue | order ...

Bootstrap Table with Vertical Heading that Responds to Different Screen Sizes

Having an issue with Bootstrap responsive table functionality: it works well for tables with horizontal headings, but I'm struggling to achieve the desired layout for my table with vertical headings. I have several 2-column tables for specifications ...

The unexpected syntax error, indicated by an ampersand followed by a quotation mark, was encountered in [location] on line 7

Every time I try to run my code, I encounter the following error message: Parse error: syntax error, unexpected '"' in D:\server\WT-NMP\WWW\myproj1\insert.php on line 7 This is the code that is triggering the error: &l ...

Looking to update a component in Vue 3 and Laravel 9 without the need to reload the entire webpage

Looking for a solution to refresh the header component upon clicking the logout button, in order to display the login and register options without refreshing the entire page. Any effective suggestions on how to achieve this are greatly appreciated. The l ...

conceal the HTML code from the students

As an instructor of an HTML/CSS course, I often assign tasks that require students to replicate the design of a webpage. However, providing them with direct links to the page makes it easy for them to decipher my methods and simply copy the work. Furthermo ...

What is the best way to transition an absolute positioned element from right to center?

When hovering over an overlay element, I want the <h3> tag to appear with a transition effect from right to center, similar to the example shown here. Could someone please assist me in achieving this? Thank you in advance. HTML <div class="row m ...

An issue arises when attempting to capture a screenshot with three.js, displaying the error message: "'modelViewMatrix' is a data that cannot be modified or configured."

Currently, I am utilizing three.js within my Vue application with the goal of capturing a screenshot of my scene as a blob. Initially, I tested this functionality by implementing: window.addEventListener('keydown', this.test.bind(this)); Within ...

Is your Javascript navigation functioning properly on some submenus while encountering issues on others?

If you visit , you'll notice that it's a visually appealing site. The navigation submenus seem to be functioning properly, HOWEVER upon inspecting the element, you'll notice that under the PRICING tab, there are submenus that have the same c ...

Unable to incorporate additional functions while using directives

http://example.com Is there a way to incorporate an addChild(tree) function into this code in order to expand the data array? I have made changes to the template as shown below: '<p>{{ family.name }}{{test }}</p>'+ '<ul>& ...

Implementing dynamic content updating in WordPress by passing variables and utilizing AJAX

Currently, I am working on a shopping page that displays a list of all the stores. To streamline the user experience, I have created a sidebar containing various categories and implemented pagination within the store listings. These lists are generated thr ...

Failure of Bootstrap tooltips to function properly on iOS and Android devices

My Bootstrap tooltips with popper.js are functioning perfectly on desktop Chrome, Firefox, and Edge. However, I'm facing issues with getting them to work on iOS (iPhone and iPad), Android (Nokia Blue), and various BrowserStack devices. Here's the ...

Is there a way to generate embedded video code using PHP?

I am in need of a PHP code that can convert URLs from various video platforms such as Youtube, Dailymotion, Vimeo, Metacafe, etc. into embeddable codes so I can display them using the echo function. Reading through each API for these websites is challengin ...

What is the proper way to mention JavaScript packages when including them as parameters in Angular elements within HTML?

I was looking to enhance the command to be more authoritative. <div (click)="lastCall(999)">click me</div> My attempt to utilize Number.MAX_SAFE_INTEGER resulted in an error from my computer stating that it wasn't recognized. As a result ...

Stop the floating left div from wrapping onto a new line

My frustration is mounting as I have 4 divs set to float left, but the last one keeps wrapping onto a new line on smaller screens. I want them to scale with the screen size so they always stay on the same line, regardless of the screen size. I'm tryin ...

Encountering issues with Font Awesome loading while using a Python simple HTTP server

Looking for help with displaying the thumbs down icon using font awesome in a simple HTML file. <html> <script src="//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"></script> <script> WebFont.load({ custom: { ...