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 1080381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:01:16+00:00 2026-05-16T22:01:16+00:00

I’m using $(‘#container_div’).load(url) to populate a div via ajax. I would like to animate

  • 0

I’m using $(‘#container_div’).load(url) to populate a div via ajax. I would like to animate the height to the height of the returned content but really cant figure out how to achieve this.

I’ve tried using something like this:

$('#main').fadeOut(function() {

 $('#main').load(url, function(data) {
     var newHeight = $(data).height();
        $('#main').animate({height:newHeight}, function() {$('#main').fadeIn();});
     });
 });

But can see that this is wrong on so many levels. Especially due to the fact that newHeight === undefined.

Can anybody point me in the right direction here? I would be eternally grateful.

  • 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-05-16T22:01:17+00:00Added an answer on May 16, 2026 at 10:01 pm

    Since fadeOut() finishes by hiding the target elements, chances are your #main will be completely hidden by the time your new data is loaded, rendering any animation of height invisible, and therefore pointless.

    But you could just use something like $('#main').show(400) which will animate the element from a size of (0,0) and opacity of 0 to whatever size is allowed by the container and contents and a fully-visible opacity of 1 (and run these animations in parallel, making both of them visible).

    But assuming you do care more about animating the height than you do about fading, you still have a problem: by the time load() calls its callback, the height of the target element(s) will already be the height of the content (or as close as possible to it). So animating won’t do anything.

    I posted a plugin on a previous question that will do what you want, but you’ll need to use $.get() instead of load():

    $.get(url, function(data) {
      $('#main').showHtml(data);
    });
    

    …where showHtml is defined as:

    // Animates the dimensional changes resulting from altering element contents
    // Usage examples: 
    //    $("#myElement").showHtml("new HTML contents");
    //    $("div").showHtml("new HTML contents", 400);
    //    $(".className").showHtml("new HTML contents", 400, 
    //                    function() {/* on completion */});
    (function($)
    {
       $.fn.showHtml = function(html, speed, callback)
       {
          return this.each(function()
          {
             // The element to be modified
             var el = $(this);
    
             // Preserve the original values of width and height - they'll need 
             // to be modified during the animation, but can be restored once
             // the animation has completed.
             var finish = {width: this.style.width, height: this.style.height};
    
             // The original width and height represented as pixel values.
             // These will only be the same as `finish` if this element had its
             // dimensions specified explicitly and in pixels. Of course, if that 
             // was done then this entire routine is pointless, as the dimensions 
             // won't change when the content is changed.
             var cur = {width: el.width()+'px', height: el.height()+'px'};
    
             // Modify the element's contents. Element will resize.
             el.html(html);
    
             // Capture the final dimensions of the element 
             // (with initial style settings still in effect)
             var next = {width: el.width()+'px', height: el.height()+'px'};
    
             el .css(cur) // restore initial dimensions
                .animate(next, speed, function()  // animate to final dimensions
                {
                   el.css(finish); // restore initial style settings
                   if ( $.isFunction(callback) ) callback();
                });
          });
       };
    
    
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.