Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:17:13+00:00 2026-06-04T17:17:13+00:00

How do I toggle the width of a div with animation? My goal is

  • 0

How do I toggle the width of a div with animation? My goal is to change the width when I click on the blue div.

$(document).ready(function() {
  $('#toggle-button').click(function() {
    $('#toggle').toggle(function() {
      $('#toggle').animate({
        width: "200px"
      });
    }, function() {
      $('#toggle').animate({
        width: "300px"
      });
    });
  });
});
#toggle {
  height: 200px;
  width: 200px;
  background: red;
}

#toggle-button {
  height: 20px;
  width: 20px;
  background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="toggle-button"></div>
<div id="toggle"></div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:17:15+00:00Added an answer on June 4, 2026 at 5:17 pm

    The issue with your code is due to the use of the (now deprecated) toggle() event handler. You can implement the logic yourself using click(), though:

    $(document).ready(function() {
      $('#toggle-button').click(function() {
        var toggleWidth = $("#toggle").width() == 300 ? "200px" : "300px";
        $('#toggle').animate({ width: toggleWidth });
      });
    });
    #toggle {
      height: 200px;
      width: 200px;
      background: red;
    }
    
    #toggle-button {
      height: 20px;
      width: 20px;
      background: blue;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <div id="toggle-button"></div>
    <div id="toggle"></div>

    – 2022 Update –

    The better approach would now be to toggle the class on successive clicks, using toggleClass(), and use CSS transition to animate the width:

    jQuery($ => {
     $('#toggle-button').on('click', e => {
      $('#toggle').toggleClass('foo');
     });
    });
    #toggle {
      height: 200px;
      width: 200px;
      background: red;
      transition: width 0.3s;
    }
    
    #toggle.foo {
      width: 300px;
    }
    
    #toggle-button {
      height: 20px;
      width: 20px;
      background: blue;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <div id="toggle-button"></div>
    <div id="toggle"></div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(function(){ $(.square).toggle( function(){ $(this).animate({width:200px}, 900); }, function(){ $(this).animate({width:100px}, 900); } ); $(document).click(function() { $('.square').animate({
The code like this: $(.hide).click(function(){ $(.online-contact).animate({width: 'toggle',height: '100%'}, fast); $(this).hide(); $(.show-class).show(); }) I want
function toggle(){ $(#tempc).toggle( function () { $(#tempc).animate({width: 255, height: 220}, 1000); $(#tempc).html(); $(#tempc).css(background-color, transparent);
http://jsfiddle.net/yrM3H/2/ I have the following code: jQuery(document).ready(function() { jQuery(.toggle).next(.hidden).hide(); jQuery(.toggle).click(function() { $('.active').toggleClass('active').next('.hidden').slideToggle(300); $(this).toggleClass('active').next().slideToggle(fast); });
Um, why no worky? Trying to toggle a ul with this: $(#others).children(.btn).click(function(){ if ($(this).children(ul).is(:hidden))
I have a jquery animation that toggles the width of a div. Within the
$(div#whitebox p).click(function () { $(div#blackout).fadeTo(550, 0).delay(555).hide(); }); What I'm trying to accomplish with the
Click the button to center div, click again to left align. This works in
here is small code <div id=clickme> Click here </div> <img id=book src=book.png alt= width=100
I have a file- Site.js with the following method: function Init() { $(document).ready(function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.