Complete guide on updating table styles by implementing a dark mode toggle button

I recently implemented a dark mode toggle button in my project after watching a tutorial on YouTube. Here is the HTML code for it:

<div class="dark-div">
     <input type="checkbox" class="checkbox" id="chk" />
     <label class="label" for="chk">
            <i class="fas fa-moon"></i>
            <i class="fas fa-sun"></i>
            <div class="ball"></div>
         </label>
     </div>

My goal is to dynamically change the table class name when the dark mode button is clicked. The table uses Bootstrap classes, and I specifically want to switch from one Bootstrap class to another (for a dark table).

This is how my table is defined in HTML:

<table id="myTable" class="sortable table table-striped table-hover dataframe">

I need to change the class to "sortable table table-dark table-hover dataframe" upon clicking the dark mode button.

The JavaScript code for the dark mode toggle button looks like this:

<script>
        const chk = document.getElementById('chk');

chk.addEventListener('change', () => {
    document.body.classList.toggle('dark');
});
    </script>

Answer №1

Instead of toggling the class on the body, make sure to target your table element in the event listener callback:

document.getElementById('myTable').classList.toggle('table-dark');

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

Issue with Bootstrap/Bootswatch Dropdown Tab Not Showing Up

I am encountering an issue while trying to implement a tab with a dropdown navigation, similar to the examples shown on Bootswatch. Interestingly, it seems that this functionality is not working properly even on the demo page provided by Bootswatch. My ma ...

Pass a selected object to a dropdown/select change event using AngularJS

Plunkr : http://plnkr.co/edit/BRQ3I4hFTlgKq4Shz19v?p=preview I'm attempting to pass the selected item from a dropdown to a function within my controller. Unfortunately, I keep receiving it as undefined. HTML : <!DOCTYPE html> <html> ...

Tips on using object fields as directive arguments in AngularJS

Is there a way for me to pass an object array to a directive and have it output specific fields that I specify at the location where I use the directive? Let's look at an example: //directive app.directive('MyDirective', function() { ret ...

Challenges with resizing images in SVG using D3

I have an SVG image created using d3. I am facing an issue where the svg is extending beyond its parent div container. Here is the code snippet: <div id="test" style="{width: 500px; height:500px;}"> <svg></svg> </div> ...

React Material UI - DataGrid Continuously Distracting Focus Away from Input Field

I'm currently facing an issue with my React web app using Material-UI. The problem arises when I try to type in the search input field, but the focus keeps getting stolen by the Material-UI DataGrid whenever I click on it. Oddly enough, this only occu ...

Error Occurs When Attempting to Call ASPX Codebehind Function from JavaScript

I am having trouble calling a function in the codebehind of my .aspx page once the window is fully displayed. I attempted to use the following code: <script type="text/javascript"> $(document).ready(function () { PageMethods.CheckFor ...

How can I make a Vue component close when clicking outside of it?

I am searching for a solution to automatically close a component when there is a click outside of the element. I attempted to use an addEventListener for this purpose. Although it successfully closes the component, it encounters an issue where it fails to ...

File remains visible after deletion upon refreshing the page, but disappears after using the Ctrl + F5

After deleting a file on the server, I noticed that it still appeared when I refreshed the page. However, when I tried to do a hard refresh by pressing ctrl+F5, it resulted in a 404 error. This behavior is puzzling to me - shouldn't a simple refresh s ...

Footer mysteriously appears on top of the container

Trying my hand at practicing HTML by creating a small webpage, but struggling with why the Footer is aligning itself under the Header instead of the container I set up. It seems like a small detail that I might have missed, but I've hit a wall and ca ...

What is the best way to add line breaks when writing text to a CSV file?

Currently, I have product descriptions stored in an object as strings. My goal is to transfer this data to a CSV file for further data analysis. The challenge lies in making the data visually appealing for easy readability. Thus far, all I've managed ...

Adjust picture dimensions when the window changes (using jQuery)

Hey there, I'm in need of some help resizing images on my webpage. I want the images to adjust their size when the page loads or when the browser is resized. The images can be either portrait or landscape, and they will be contained within a div that ...

I would like to connect the button to a different webpage

Is it possible to connect this code with page number 2? <div class="login"> <h1>Login</h1> <form method="post"> <input type="text" name="u" placeholder="Username" required="required" /> <input type="password" name="p" ...

My controller for angular seems to be malfunctioning

I am fairly new to AngularJS. Although I have created a controller and passed the data, it seems that my controller is not functioning properly. Can someone please assist me? This is my code: The AngularJS code snippet looks like this: var app = angula ...

What's the best way to identify the origin of a custom tag in a library?

I have a unique question that I need help with, so please bear with me. I recently started working on a new project at my job and it's built on some older technologies (Vue2, no ts support, etc.). It also relies on a few different UI libraries and de ...

Can MooTools be used for asynchronous file uploads?

Currently, I am working on file uploading using asp.net: <asp:FileUpload ID="Upload" runat="server" /> <!-- HTML --> Upload.PostedFile.SaveAs(physicalPath + "newAvatarTemp.png"); // codebehind However, I find it frustrating when pages need to ...

React Hooks for Navigation in a Webpage (Auto-Resetting)

Trying to manage the display of different questions within a long form using Hooks has been a challenge. Utilizing useState for state management, the first two pages function correctly. However, upon clicking on the third page, it briefly flashes on the ...

Modify the color of the text for the initial letter appearing in the sentence

I am currently exploring the possibility of altering the font color of the initial instance of a letter in a div. For example, if the String were 'The text' and I desired to make the color of 'e' yellow, then only the first e would be i ...

Once the notification for file upload is received, promptly close the modal pop-up

I'm facing an issue with closing a modal pop up after successfully uploading a file using an IFRAME. I've tried the following code snippet, but it doesn't seem to work: function CloseWindowFunction() { alert('PDF uploaded succe ...

The divs are crashing into each other and moving at varying speeds

I have created a mini game inspired by diep.io on codepen, where you can upgrade your reload and shooting capabilities. However, I have encountered an issue where the bullets start moving at different speeds and end up overlapping each other after shooting ...

When attempting to use JSON.parse on a basic object, I keep encountering an error labeled as "Unexpected token"

I'm facing an issue with the following code snippet: var a = localStorageService.get('selectedQuestionSortOrder'); $scope.selectedQuestionOrderBy = JSON.parse(a); var b = 99; Upon inspecting with a debugger, I observe the following: a - O ...