Toggle switch experiencing issues following removal of an h1 tag

After deleting an h1 element, my toggle switch started malfunctioning. Initially centered within the slider, it is now offset to the top. However, clicking the button corrects its position temporarily. Upon refreshing the page, the offset issue reappears.

What could be causing this problem?

function SlideRight() {
  // Checks to see if the slider is to the left of the div
  if (document.getElementById("slider").style.float !== "right") {
    // If it is we will float the sliderBtn to the right and change the background of the housing to green
    document.getElementById("slider").style.float = "right";
    document.getElementById("slideHousing").style.backgroundColor = "#00ff00";

    // Toggle dark mode on
    document.body.style.backgroundColor = "#595959";
    document.getElementById("header").style.color = "#e6e6e6";
    document.getElementById("press").style.color = "#e6e6e6";
  } else {
    // If clicked again the btn will move back to the left side and change the color back to original
    document.getElementById("slider").style.float = "left";
    document.getElementById("slideHousing").style.backgroundColor = "#f2f2f2";

    // Toggle dark mode off
    document.body.style.backgroundColor = "#e6e6e6";
    document.getElementById("header").style.color = "#000";
    document.getElementById("press").style.color = "#000";
  }
}
body {
  height: 100%;
  margin: 0;
  padding: 0;
  background-color: #e6e6e6;
}

html {
  height: 100%;
}

.main {
  display: table;
  height: 100%;
  width: 100%;
  border: 1px solid transparent;
}

.container {
  display: table-cell;
  vertical-align: middle;
  border: 1px solid transparent;
}

.slider {
  height: 100px;
  width: 200px;
  border-radius: 50px;
  background-color: #f2f2f2;
  margin: 0 auto;
  border: none;
  box-shadow: inset 0 0 7px #000;
}

.slideBtn {
  border: 1px solid transparent;
  height: 90px;
  margin-top: 4px;
  margin-left: 5px;
  margin-right: 5px;
  width: 90px;
  border-radius: 50px;
  background-color: silver;
  box-shadow: 0 0 5px #000;
}
<h1 style="text-align: center;" id="header">Dark Mode</h1>
<div class="main">
    <div class="container">
        <p style="text-align: center;" id="press">Press button to toggle dark mode.</p>
        <div class="slider" id="slideHousing">
            <div class="slideBtn" id="slider" onclick="SlideRight()">

            </div>
        </div>
    </div>
</div>

Answer №1

To make the button float correctly, ensure you set the initial value of float.

function SlideRight() {
  // Check if the slider is positioned to the left of the container
  if (document.getElementById("slider").style.float !== "right") {
    // Float the sliderBtn to the right and change the background color to green
    document.getElementById("slider").style.float = "right";
    document.getElementById("slideHousing").style.backgroundColor = "#00ff00";

    // Toggle dark mode on
    document.body.style.backgroundColor = "#595959";
    document.getElementById("header").style.color = "#e6e6e6";
    document.getElementById("press").style.color = "#e6e6e6";
  } else {
    // Move the btn back to the left and revert colors when clicked again
    document.getElementById("slider").style.float = "left";
    document.getElementById("slideHousing").style.backgroundColor = "#f2f2f2";

    // Turn off dark mode
    document.body.style.backgroundColor = "#e6e6e6";
    document.getElementById("header").style.color = "#000";
    document.getElementById("press").style.color = "#000";
  }
}
body {
  height: 100%;
  margin: 0;
  padding: 0;
  background-color: #e6e6e6;
}

html {
  height: 100%;
}

.main {
  display: table;
  height: 100%;
  width: 100%;
  border: 1px solid transparent;
}

.container {
  display: table-cell;
  vertical-align: middle;
  border: 1px solid transparent;
}

.slider {
  height: 100px;
  width: 200px;
  border-radius: 50px;
  background-color: #f2f2f2;
  margin: 0 auto;
  border: none;
  box-shadow: inset 0 0 7px #000;
}

.slideBtn {
  float:left;
  border: 1px solid transparent;
  height: 90px;
  margin-top: 4px;
  margin-left: 5px;
  margin-right: 5px;
  width: 90px;
  border-radius: 50px;
  background-color: silver;
  box-shadow: 0 0 5px #000;
}
<h1 style="text-align: center;" id="header">Dark Mode</h1>
<div class="main">
    <div class="container">
        <p style="text-align: center;" id="press">Press button to toggle dark mode.</p>
        <div class="slider" id="slideHousing">
            <div class="slideBtn" id="slider" onclick="SlideRight()">

            </div>
        </div>
    </div>
</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

Assign a class to the "li" element containing an "a" tag with the specified href attribute

Is it possible to transform the "href" attribute of an element into a class or id like "li" for better css handling? <ul> <li><a href="#section3"><span class="fp-sr-only">due</span><span></span></a><d ...

Utilizing Selenium IDE and XPath to confirm that a checkbox is selected only when the associated label contains certain text

I need assistance in creating an Xpath query to validate if a specific checkbox is checked within the nested divs of the panel-body div. My goal is to ensure that a checkbox with the label "Evaluations" contains the class "checked". Below is the snippet of ...

Using jQuery to Verify Matching Objects Based on a Shared Property in Sets

I am in search of a solution using JavaScript or jQuery to ensure that two sets/arrays of objects share the same value for a particular property. In this case, two items share the same guid value across both sets, but some properties differ between them - ...

Once the script is imported, the functionality of the bootstrap slider ceases to operate

Once the script is imported, the bootstrap slider ceases to function properly. Adding this script causes the issue: <script src="http://echarts.baidu.com/build/dist/echarts.js"></script> An error appears in the console: jquery.min.js:3 Unca ...

Combining React Components and HTML Elements

I've always been puzzled by the prevalence of using components in React examples. For instance, consider a scenario where I only need to combine some basic HTML with my components: class Homepage extends Component { render() { return ( &l ...

JavaScript, detecting repeated characters

I need to create a script that checks an input box (password) for the occurrence of the same character appearing twice. This script should work alongside existing regex validation. I believe I will need to use a loop to check if any character appears twice ...

The Django framework does not apply color to the Bootstrap Navbar as expected

I'm currently working on building an E-Commerce Website using Django. I have implemented a Bootstrap navbar, but the CSS styles that I obtained from this source are not functioning properly when placed within the {%block css %} tag. I have attached t ...

The show/hide feature is malfunctioning

I need help with my PHP page where I display multiple choice questions from a database along with options and answers. Currently, I am using an HTML button to show/hide the answers but it only works for the first question. When I click on other questions, ...

Using CSS grid to completely fill a 100vh area without impacting the height of the rows

I am working on a grid layout with 3 columns and a variable number of rows. The background of the grid is colored, but when there are not enough rows to fill the vertical space, it remains uncolored: .grid { align-self: flex-start; display: grid; ...

Cannot load local JSON file via AJAX in ReactJS

Below is the React component I am currently working with. import React, { Component } from 'react'; class GraphView extends Component { constructor(props){ super(props); this.state = { datajson: &apo ...

How can I trigger a directive multiple times by clicking on a node in Angular JS?

I have implemented a custom directive to refresh my page, utilizing D3 for my UI. The graph consists of nodes and links. My goal is to have a node expand into its subgraph when clicked, and so on. Below is the corresponding controller code: vrmUI.contro ...

Ways to abbreviate a URL using Next JS

While working on my Next.js application, I encountered an issue with passing an object between pages. To achieve this, I compressed my array of objects into JSON using JSON.stringify(result) on the index page and then parsed it on the second page using JSO ...

Generating a web page using MVC's html.raw and receiving the message "Apologies, but an error has occurred during the

In my MVC application, the /Views/Content/Details.cshtml page contains the following code: @model example.Models.Content @{ ViewBag.Title = "Details"; } @Html.DisplayFor(model => model.Description) Everything functions as expected. The browser d ...

Issue with Observable binding, not receiving all child property values in View

When viewing my knockout bound view, I noticed that not all values are being displayed. Here is the script file I am using: var ViewModel = function () { var self = this; self.games = ko.observableArray(); self.error = ko.observable(); se ...

Is there a way to circumvent the eg header along with its contents and HTML code using JavaScript?

I have a script in JavaScript that is designed to replace the content of data-src attribute within each img tag with src. However, I am facing an issue where this script interferes with the functionality of a slider located in the header section. The sli ...

Incorporate a CSS class name with a TypeScript property in Angular version 7

Struggling with something seemingly simple... All I need is for my span tag to take on a class called "store" from a variable in my .ts file: <span [ngClass]="{'flag-icon': true, 'my_property_in_TS': true}"></span> I&apos ...

Tips for sorting through and minimizing data based on the most recent date

info = { start: 1, data: [ { name: 'Maria', date: '2020-02-15 }, { name: 'Paula', date: '2020-06-10 }, { name: 'Eva', date: '2020-12-05 }, { name: 'Sophia', date ...

A step-by-step guide on modifying the box-shadow color using jquery

I have developed some JavaScript code that adjusts the box-shadow of buttons to be a darker version of their background color. This allows users to dynamically change the button background colors. The current code successfully changes the box shadow based ...

I am having trouble adjusting my web page to fit the screen fluidly as it appears too large and requires horizontal scrolling

As a student learning html/css, I am currently facing some challenges with my first website. My main issue is setting up the page to be fluid rather than fixed. The box, image, and text are all configured to be absolute on the page, but I cannot seem to ma ...

Steps for incorporating Bootstrap 5 pagination without using jQuery

Currently, I am working on incorporating pagination for cards using Bootstrap 5. My reference point is the following link: https://getbootstrap.com/docs/5.0/components/pagination/ While I can find the UI elements, I am struggling to come across a concrete ...