Ask Your Question
3

How can we implement a dark mode using jQuery on an HTML page?

asked 2023-06-17 01:37:49 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-17 02:01:02 +0000

lakamha gravatar image

Here is a basic example of implementing a dark mode using jQuery:

HTML:

<body>
  <div id="main-content">
    <h1>Welcome to my website</h1>
    <p>This is some sample text</p>
    <button id="dark-mode-btn">Toggle Dark Mode</button>
  </div>
</body>

CSS:

body {
  background-color: #f2f2f2;
  color: #333;
}

.dark-mode {
  background-color: #333;
  color: #f2f2f2;
}

jQuery:

$(document).ready(function() {
  // toggle dark mode on button click
  $('#dark-mode-btn').click(function() {
    $('body').toggleClass('dark-mode');
  });
});

In this example, we have a button with an ID of "dark-mode-btn" that toggles the "dark-mode" class on the body element when clicked. The "dark-mode" class applies a dark background color and light text color to create a dark mode effect. You can customize the dark mode styles to fit your specific needs.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-17 01:37:49 +0000

Seen: 11 times

Last updated: Jun 17 '23