Having trouble with the animate() function not working properly?

I have been working on the following code snippet:

var isOn = false;

$('.switch').on("click",function(){

  if (isOn){


    $('.toggle').animate({
      left:"18px"
    },10,"linear",

     {
      complete: function(){
        $('#label').text("ON");
      }
    });
    isOn = false;



  } else {
    $('.toggle').animate({
      left:"4px"
    }, 10,"linear",
                         {
      complete: function(){
        $('#label').text("OFF");
      }
    });
    isOn = true;
  }

});

Check out the code on CodePen

This code implements a toggle switch functionality using jquery. Originally, the switch worked without the use of the animate() method.
However, I had to add the animation through jQuery as the CSS animation was causing issues in Internet Explorer.
You can see the original effect without animation here.

I am facing an issue where the complete function in the first link does not seem to be working as expected. Any insights on why this might be happening?

EDIT: The code is now functioning properly, but there are still some compatibility issues with Internet Explorer.

Answer №1

It appears that you are combining two different types of the .animate function. If you decide to provide the duration and easing directly as arguments, you must also do the same for the callback function:

$('.toggle').animate({left: "18px"}, 10, "linear", function(){
  $('#label').text("ON");
});

Alternatively, you can pass two objects:

$('.toggle').animate(
  {
     left:"18px"
  },
  {
    duration: 10,
    easing: "linear",
    complete: function(){
      $('#label').text("ON");
    }
  }
);

Answer №2

It should be functioning now. Make sure to visit the jsfiddle link provided below: http://jsfiddle.net/banded_krait/da2kE/

I made some modifications to your code by removing some brackets and the complete: array key. Hopefully, this solution works for you.

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 uploading files on Android devices in Laravel and Vue JS

My Laravel/Vue JS web app allows users to upload files and photos. Everything runs smoothly except when accessed on Android devices, where the axios call seems to get stuck indefinitely. Below is the code snippet causing the issue: Vue JS component <t ...

Bizarre CSS Transitions and Scaling Quirks in Webkit

Working on a website, you can check it out here In Chrome, some strange behavior occurs when the window reaches a certain resolution. Specifically, at 943 pixels width, white bars appear next to images when hovering over them, giving the impression that t ...

"Looping through each item in a list using Angular

I have a setup for an HTTP request that will return the list of products once all promises are fulfilled. My objective is to initially set the opacity of these products to 0, and then use a forEach loop to add a class that sets their opacity to 1. While ...

Trouble Logging In: User Login Issue with SailsJS and PassportJS Plugin (sails-generate-auth)

I'm currently facing an issue with user authentication in my SailsJS app using PassportJS. I followed a tutorial on setting up authentication in SailsJS using sails-generate-auth, which can be found here. The POST request seems to be routed correctl ...

Utilizing JavaScript to manage sections within a dropdown menu

When dealing with this particular HTML code, there is a feature where a list item includes options such as all, a, b, c, and d. If the user selects 'All', it should restrict them from choosing any other items. However, if they do not choose &apos ...

Challenge with row identification in Datatables when rowId begins with a number

In compliance with the Datatables specifications, each row in my table can be assigned a unique ID: $('#myTable').DataTable( { ajax: '/api/staff', rowId: 'staffId' } ); However, it is mentioned in the same specificat ...

Ways to send a POST variable to PHP without refreshing the webpage

Is there a way to pass a simple variable from a text-box on a different page to PHP without reloading it? I've been informed that Ajax is required for this task, but I'm unsure of how to go about implementing it. Can someone provide me with a sam ...

Enhance the function for handling AJAX responses

Utilizing this code allows for the handling of responses from an RSS feed. The code efficiently organizes and appends content, separating any embedded videos. While seeking feedback primarily on performance/efficiency, I am also open to other suggestions. ...

What is the best way to ensure an observable has been updated before proceeding with additional code execution?

Is an observable the best choice for providing live updates of a variable's value to another class? I have a loop in my service class that looks like this: elements.forEach(element => { doStuff(); this.numberSubject.next(valueFromDoStuff); }) ...

Leveraging python capabilities within html documents

English is not my first language, so please excuse any spelling errors. I am working on combining HTML and Python to develop a graphical user interface (GUI) that can communicate with a bus system (specifically KNX bus). Currently, I have a Raspberry Pi ...

Links arranged in semicircles along the perimeter of the page

Here is some code that I have created to display two links in circles positioned on the left and right sides of a webpage. [class*="navigation"] .nav-previous, [class*="navigation"] .nav-next { position: fixed; z-index: 999; top: 50%; text-align ...

Execute index.js code from index.html using NodeJS and Express

Currently, I am diving into the world of NodeJS and Express using Replit.com for a small project. The main objective is to develop a basic input field that, upon submission, will post to different channels such as Discord and Twitter. The piece of code be ...

Mobile users are unable to access the form

Welcome to my website ! I encounter an issue where I can successfully submit a form on the desktop version, but it seems that the "Next" button below the form is not clickable on mobile. I would greatly appreciate any assistance in resolving this matter. ...

Error: Invariant violation - App component did not return anything in the render method

I am facing an issue while attempting to render a component based on the promise returned from AsyncStorage. The error message I receive is: Error: Invariant Violation: App(...): No content was returned from the render method. This typically indicates ...

CustomJS TextBox callback to adjust range of x-axis: Bokeh

I'm currently working on a web page that features a plot powered by an AjaxDataSource object. However, I am facing a challenge with implementing a TextInput widget that can modify the xrange of this plot. Here is a snippet of my code: source = AjaxDa ...

Disable a button during an AJAX request

Whenever a button is clicked, the record is saved to the database without refreshing the page using AJAX. I have implemented the AJAX code successfully. Now I need guidance on how to manage the state of the Submit button - enabling/disabling it dynamicall ...

Retrieving information from a MYSQL database table and populating a text field with AJAX and PHP

Is there a way to use AJAX to display the data from an array that is called in getword.php into a text field in index.php? Let's take a look at the code below: Here is the code snippet from index.php: <body> <div class="container" ...

Problem with Autofill function in ASP.NET MVC 5

I have recently set up an ASP.NET MVC5 project and have included some links in order to enable an autocomplete jQuery plugin. _Layout page <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-s ...

What is the process for removing the highlighted border from a navigation menu in ASP.NET?

I am currently utilizing the navigation menu in the ASP.NET toolbox, but I am struggling to remove an unwanted golden border. Despite my efforts, I have not been able to find any information on how to resolve this issue. The golden border only appears when ...

Using vanilla JavaScript in a node.js environment alongside Functional Reactive Programming (FRP) with bacon.js

I am interested in incorporating Functional Reactive Programming (FRP) into my project. I have come across a popular library for node.js called bacon.js that implements FRP. However, I am having trouble finding examples of how to use bacon.js in native J ...