---
title: "How to use the toggle function with click function?"  
description: "How to use the toggle function with click function?"  
author: "Jayden Bell"  
published: 2013-06-06  
updated: 2013-06-06  
canonical: https://www.mindstick.com/forum/998/how-to-use-the-toggle-function-with-click-function  
category: "jquery"  
tags: ["jquery"]  
reading_time: 1 minute  

---

# How to use the toggle function with click function?

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),\
I like to create a animate when user [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) on a [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net).There are two animate [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) I like to do on click so I thought to use [toggle](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) function which [swap](https://www.mindstick.com/forum/34027/how-to-swap-the-values-of-two-columns-in-sql-server) between two function.\
Now [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is if I use toggle function it doing the two animate function without click and also the button disappears.\
**$('#button').toggle(** **function() {** **$('#slider').stop().animate({"[margin](https://answers.mindstick.com/qa/104990/what-is-a-margin-call)-[right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not)": '0'});** **},** **function() {** **$('#slider').stop().animate({"margin-right": '50'});** **}****);**\
Here slider is the div which animates.\
How to do the animate [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) alternatively every time when user click a button?\
Thanks in advance.

## Replies

### Reply by AVADHESH PATEL

Hi,\
You can try as following\

```
$('#button').on('click',function(){   var mr=parseInt($('#slider').css('margin-right').replace('px',''))==0 ? 50 : 0;   $('#slider').stop().animate({"margin-right": mr});});
```

\
You can try as following\

```
var mr=0;$('#button').on('click',function(){    $('#slider').stop().animate({"margin-right": mr});    mr=(mr==0) ? 50 : 0;});
```

**\**I hope it helpful for you.


---

Original Source: https://www.mindstick.com/forum/998/how-to-use-the-toggle-function-with-click-function

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
