Javascript function causing toggle button malfunction - troubleshoot required

I am having trouble with a toggle round button and the corresponding JavaScript that I have implemented. It seems like the switch does not move, even though when I tested the link to the JS file with an alert it did work.

let touchIdN = document.getElementById('touchidN');
let touchIdY = document.getElementById('touchidY');
    
touchIdN.hidden = false;
touchIdY.hidden = true;
    
let enableTouchId = () => {
  touchIdY.hidden = false;
  touchIdN.hidden = true;
}
    
let disableTouchId = () => {
   touchIdY.hidden = true;
   touchIdN.hidden = false;
}
    
touchIdN.onclick(enableTouchId);
touchIdY.onclick(disableTouchId);

In my HTML code, #touchidY is set to display hidden using CSS for both options.

<tr>
  <td><img src='./icons/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f090601080a1d1f1d06011b2f5d17411f0108">[email protected]</a>'></td>
  <td>Enable Touch ID</td>
  <td>
    <label class="switch" id='touchidN' onclick='enableTouchId()'>
      <input type="checkbox">
      <span class="slider-round"></span>
    </label>
    <label class="switch-yellow" id='touchidY' onclick='disableTouchId()'>
      <input type="checkbox">
      <span class="slider-back"></span>
    </label>
  </td>
</tr>

The aforementioned code is included within a table in my HTML page.

Answer №1

Various mistakes detected:

  1. The presence of TWO toggles is due to having two checkboxes.
  2. Instead of invoking the function with touchIdN.onclick(enableTouchId);, you should assign it using
    touchIdN.onclick = enableTouchId;
  3. It is recommended to utilize
    touchIdN.addEventListener(<event>, enableTouchId);
  4. If you are defining the callback in the HTML with
    <label class="switch" id='touchidN' onclick='enableTouchId()'>
    , refrain from duplicating it in JS. Choose one method (HTML OR JS) and adhere to it, typically not both.

Your current code has been quickly modified for functionality, but there is room for optimization.

let touchId = document.getElementById('touchid');


let toggleTouchId = (active) => {
  console.log(active)
  if (active) {
    // Implement your enable touch procedures
  }
  else {
    // Implement your disable touch procedures
  }
}
    
touchId.addEventListener('change', function() {
  toggleTouchId(this.checked);
});
<tr>
  <td><img src='./icons/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aaccc3c4cdcfd8dad8c3c4deea98d284dac4cd">[email protected]</a>'></td>
  <td>Enable Touch ID</td>
  <td>
    <label class="switch">
      <input type="checkbox" id='touchid'>
      <span class="slider-round"></span>
    </label>
  </td>
</tr>

Furthermore, observe how W3C implements sliders; they employ CSS animations rather than JavaScript.

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

After deploying to Firebase, animations suddenly cease to function

After running npm run-script build and deploying my React app to Firebase hosting with firebase deploy, I encountered an issue where my animations stopped working. I'm puzzled as to why this is happening, especially since I've included keyframes ...

What is the best way to smoothly insert an item into a nested object within the state of a React component

Trying to create a complex array using react hook: const [strategy, setStrategy] = useState([leg]); Here's how the `leg` object is defined: const leg = { entry: { conditions: [], actions: [""], }, exit: { conditions: [], ...

Can I use the mouseover event on an image to display a sibling div?

Hi everyone! I'm fairly new to web development and I could really use some advice. I'm trying to implement a mouseenter event on an img tag to show a sibling div, but I'm encountering some strange behavior. The mouseenter event seems to be w ...

What method would you recommend for modifying HTML text that has already been loaded using JSP?

Is there a way to update text on an HTML document without reloading the entire page? I'm looking to create a simple "cart" functionality with 5 links on a page. When a link is clicked, I want it to increment the "items in cart" counter displayed on th ...

Calling an ajax request to view a JSON pyramid structure

My experience with ajax is limited, so I would appreciate detailed answers. I have a Pyramid application where I need to load information via ajax instead of pre-loading it due to feasibility issues. I want to retrieve the necessary information through a ...

Node.js encountered an error: Attempting to access properties of an object that is undefined, specifically trying to read 'TokenType'

I've encountered an issue with my node.js application. It has a simple login page where 'user1' can access a private Power BI report. Everything seems to be working fine, except when 'user1' logs in, a blank page appears with the f ...

Unsatisfied Dependencies in NPM

Despite searching extensively on SO and across the web, I have yet to find a solution to my issue. I am in the process of creating a package of React Native components for use in various other React Native projects. The goal is for other teams to simply ad ...

What is the best way to determine if a URL parameter is present when compared to an Array?

Given the following two example URLs and code snippets, how can I utilize an Array as a reference to verify if the values are present in the UTM parameter? (Note: There will only be one UTM parameter at a time). example.com?utm=test example.com?utm=test ...

Tips for Ensuring the Longevity of my Node.js Server

After developing an application in Node js with express js, I am facing an issue where the server shuts down whenever I close the command prompt. To start the app, I use the following command: c:/myApp > npm start I attempted to resolve this problem b ...

display data from a hashmap in a React component

Struggling with extracting data from a hashmap retrieved from an API for my React application. Here's a snippet of the data: [ [ "d243dc7-6fe8-151b-4ca8-1be528f2b36", "[\"Jonny\",70]" ], ...

Unable to reach the attribute while utilizing a pipe symbol

Check out my code snippet: type DetailsItemEditInput = { type: 'text' | 'number'; }; type DetailsItemEditDropdown = { type: 'dropdown'; options: []; }; type DetailsItemEdit = DetailsItemEditDropdown | DetailsItemEditIn ...

Even though the if statement has been implemented, the page continues to show null objects

So, I have an if statement that filters out null objects and it's working fine. However, when I try to insert something in join() like join("li"), all null objects are displayed on the page as shown in the image. I just want to display objects that ar ...

Troubles encountered when populating the array within a Vue component

I am experiencing an issue with my ProductCard component where the addItem method is not successfully adding a new item to the array. <template> <div class="card"> <div v-for="item in TodoItems" :key="item.id ...

Is the resolving functionality of Webpack capable of resolving path definitions in AMD style?

Currently, I am in the process of setting up a basic proof of concept. I have a JavaScript library that utilizes AMD, but I need the dependencies to be resolved in node_modules. To achieve this, I've made the following changes: Here is my webpack.con ...

Creating a VSCode extension using Vue: Step-by-step guide

I'm looking to develop a VSCode extension with Vue utilizing the VSCode webview. However, when attempting to import the compiled index.html into my extension, I consistently receive a prompt to enable JavaScript. Is there a solution for integrating Vu ...

How can I access a child element within an element using Protractor?

I'm struggling to access the child element of an ng-repeat element. After searching for solutions, I came across suggestions that didn't work for me. One tip was to try something like this: var parent = element(by.repeater('')); var c ...

Tips for updating the content of a div with the click of another div

Is there a way to dynamically change the content of a div upon clicking on a menu item? Here is the current layout for reference: https://i.stack.imgur.com/nOnlQ.png Below is the CSS and HTML code snippet: body { background-color: #555657; ...

Unlocking the potential of Node.js: Mastering the art of passing extra parameters in async

Exploring JavaScript and Node.js I'm currently working on developing some javascript / node.js code with a specific purpose in mind: To retrieve keys from a redis database and extract relevant information from the hash. Here is an example of what ...

I do not prefer output as my optimal choice

My preference is to create drill down buttons rather than focusing on output. Currently, the output appears as: https://i.sstatic.net/8qs5F.png The content of index.html is as follows: <html>  <head> <script type="text/javascript" ...

Javascript: Altering Link Color

After creating my own webpage with a unique light switch that inverts colors, I encountered an issue with changing the color of my links. Despite setting the anchor tags to transition to a different color, it just wouldn't work. Here is the HTML code ...