Struggling to make the custom javascript toggle switch function properly

I've been attempting to create a switch that, when clicked, toggles between "ski" and "snowboard."

My JavaScript code seems like it should work, but for some reason, nothing is happening.

function toggleDisplay(a,b){
document.getElementById(a).style.display = "none";
document.getElementById(b).style.display = "";
}
.switch {
  border: 1px solid black;
  background-color: rgb(220,220,220);
  padding: 2px;
  width:150px;
  height:24px;
  
}

#on {
  float:left;
  display:"";
  width: auto;
  height:20px;
  background-color: rgb(95,170,250);
}

#off {
  float:right;
  display:none;
  width: auto;
  height:20px;
  background-color: rgb(250,50,25);
}
      <div class="switch">
            <div id="on" onclick="toggleDisplay('on','off')">
          Ski
            </div>
            <div id="off" onclick="toggleDisplay('off','on')">
           Snowboard
            </div>
          </div>

Answer №1

Initially, there were a couple of errors that needed correction. Firstly, using a switch statement as a named function is invalid in JavaScript as it's a reserved keyword. Another mistake was passing 'on' and 'off' as variables instead of strings. I have rectified these issues for you. Please refer to the revised code below:

function changeState(a, b){
   document.getElementById(a).style.display = 'none';
   document.getElementById(b).style.display = 'inline-block';
}
.switch {
  border: 1px solid black;
  background-color: rgb(220,220,220);
  padding: 2px;
  width:150px;
  height:24px;
}

#on {
  float:left;
  display:"";
  width: auto;
  height:20px;
  background-color: rgb(95,170,250);
}

#off {
  float:right;
  display:none;
  width: auto;
  height:20px;
  background-color: rgb(250,50,25);
}
<div class="switch">
     <div id="on" onclick="changeState('on','off')">
        Ski
     </div>
     <div id="off" onclick="changeState('off','on')">
            Snowboard
     </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

Is there a way to prevent HTML code from being executed when sharing it as text? I want to be able to share code without it being activated as HTML on the page

Seems like I have a straightforward issue related to HTML tags. I am looking to post some code snippets on my blog, specifically the <h1> heading</h1>. I want visitors to view and not just the bolded text of heading. Do I need to incorporate J ...

Is there a way to trigger validation with a disabled property?

My form element is set up like this: <input type="text" id="country" formControlName="Country" /> The form group looks like this: this.myForm = this.formbuilder.group({ 'Country': [{ value: this.user.Country, disabled: this.SomeProperty= ...

Cause a bootstrap column to have overflowing content rather than expanding

I've searched extensively for the solution to my problem, but haven't found a satisfactory answer yet. My goal is to create a location finder with a map on the right and a list on the left. With over 18,000 locations to search through, the list ...

Is there a way to use jQuery draggable to move a zoomed image within a div clipping mask?

Looking to enhance user experience, I am developing an interface that allows users to zoom in on an image and move the zoomed version within a clipping mask div. I have created a div with dimensions of 200px by 200px and set overflow: hidden. Then, I inse ...

Transferring state to a nested child component within a parent component's child

Currently, I am in the process of developing a KanBan application using ReactJS. My main goal is to propagate the state from a parent component to the farthest child component in the hierarchy. Within my main App component, there exists a Column component, ...

When the React-bootstrap Popover arrow is oriented vertically, the arrow colors appear to be inverted compared to when it

Utilizing a react-bootstrap Popover component, I have implemented custom styling for the arrow with a gray color and 50% opacity. The CSS code that enables this customization is as shown below: .popover .popover-arrow::before, .popover .popover-arrow::afte ...

Make the div's width equal to the width of the table inside

In one of my table cells, I have the following structure: <td> <div class="table-wrapper"> <table class="inner-table"> <!--content--> </table> <div> </td> I added the div so I coul ...

Struggling to make the javascript function compatible with the drop-down menu

I'm encountering issues with making my JavaScript work properly alongside my HTML. Specifically, I want the "activity" drop-down box to function in conjunction with the "city" drop-down box. For instance, if I choose "Brisbane" and then select an acti ...

The issue with Angular-gettext is that it fails to update dynamically generated strings in the code

Whenever I try to set the language using the code gettextCatalog.setCurrentLanguage(langString);, it doesn't seem to affect my side-nav menu. My side menu has two possible states: expanded or collapsed, and I use ng-include to control its content base ...

The Vue router requires a page reload to see changes and does not have access to the this

When navigating through router-link, the App.vue page opens initially, but the URL changes immediately without displaying the content. Reloading the page is necessary to view it. If you navigate programmatically, you may encounter an error stating 'Ca ...

What causes the disparity in appearance between this html/css code in firefox versus ie?

css: * { margin:0; padding:0; } .blue-button { width:auto; display:inline-block; } .blue-button:before { /*background-image:url('blue-button.gif');*/ background:red; ...

Integrating a PHP function within an ECHO statement

Here is a code snippet I'm currently using in my Wordpress: function wpstudio_doctype() { $content = '<!DOCTYPE html>' . "\n"; $content .= '<html ' . language_attributes() . '>'; echo apply_filte ...

Engage with a specific element within the Document Object Model even in the absence of a 'ref' attribute

Is there a better way to set focus on a button within a React component without using ref? The button is part of a third-party library and not immediately available upon componentDidMount. Currently, I am using setTimeout and querySelector which feels like ...

AJAX: How to handle a successful POST request?

I have been recently exploring AJAX and I can see why I hesitated to delve into this particular area of JavaScript; it appears quite intricate. Most discussions seem centered around how to SEND data via POST, with little focus on what happens once the POS ...

Unable to assign headers once they have already been sent to the recipient - a Node.js error

Encountering an error message stating "Cannot set headers after they are sent to the client." I've researched and it seems like multiple callbacks may be causing this issue. However, I'm struggling to find a solution. Any assistance in resolving ...

Show/Hide Section

I am struggling to implement two buttons - one for expanding a div and one for collapsing it. Despite multiple attempts to modify the code, I have not been successful. It appears that I am having trouble understanding how to prevent a link from toggling. ...

CSS tip: Create a trendy design by adding a slanted border to the last

I need help creating a unique menu list with a white border design. The borders should all be straight by default, except for the last border which must be slanted. ul { background-color: #183650; } li { list-style: none; display: inline-block; b ...

Error message: ASP.Net Update Panel unable to access global variables in code-behind

I have been working on updating a bootstrap progress bar's percent on my website. While I understand that ajax is the recommended method for this, I attempted to achieve this without using ajax and was able to make some progress. Below is the code sn ...

Set up specific vue.config.js configurations for unique environments in Vue

I am working on a multi-page app where I want certain pages to only show up in my development environment. Here's how my vue.config.js looks: module.exports = { productionSourceMap: false, pages: { index: "src/main.js", admin: { ...

Can a contentEditable div be given a set height in React.js?

I have a contentEditable div that I want to limit in size. The user should not be able to continue typing once the capacity is reached, and no new lines should be added beyond the fixed height. For example, if the div's height is 600px and the user tr ...