Change text (html) with .animate

116,848

Solution 1

The animate(..) function' signature is:

.animate( properties, options );

And it says the following about the parameter properties:

properties A map of CSS properties that the animation will move toward.

text is not a CSS property, this is why the function isn't working as you expected.

Do you want to fade the text out? Do you want to move it? I might be able to provide an alternative.

Have a look at the following fiddle.

Solution 2

$("#test").hide(100, function() {
    $(this).html("......").show(100);
});

Updated:

Another easy way:

$("#test").fadeOut(400, function() {
    $(this).html("......").fadeIn(400);
});

Solution 3

See Davion's anwser in this post: https://stackoverflow.com/a/26429849/1804068

HTML:

<div class="parent">
    <span id="mySpan">Something in English</span>
</div>

JQUERY

$('#mySpan').animate({'opacity': 0}, 400, function(){
        $(this).html('Something in Spanish').animate({'opacity': 1}, 400);    
    });

Live example

Solution 4

Following the suggestion by JiminP....

I made a jsFiddle that will "smoothly" transition between two spans in case anyone is interested in seeing this in action. You have two main options:

  1. one span fades out at the same time as the other span is fading in
  2. one span fades out followed by the other span fading in.

The first time you click the button, number 1 above will occur. The second time you click the button, number 2 will occur. (I did this so you can visually compare the two effects.)

Try it Out: http://jsfiddle.net/jWcLz/594/

Details:

Number 1 above (the more difficult effect) is accomplished by positioning the spans directly on top of each other via CSS with absolute positioning. Also, the jQuery animates are not chained together, so that they can execute at the same time.

HTML

<div class="onTopOfEachOther">
    <span id='a'>Hello</span>
    <span id='b' style="display: none;">Goodbye</span>
</div>

<br />
<br />

<input type="button" id="btnTest" value="Run Test" />

CSS

.onTopOfEachOther {
    position: relative;
}
.onTopOfEachOther span {
    position: absolute;
    top: 0px;
    left: 0px;
}

JavaScript

$('#btnTest').click(function() {        
    fadeSwitchElements('a', 'b');    
}); 

function fadeSwitchElements(id1, id2)
{
    var element1 = $('#' + id1);
    var element2 = $('#' + id2);

    if(element1.is(':visible'))
    {
        element1.fadeToggle(500);
        element2.fadeToggle(500);
    }
    else
    {
         element2.fadeToggle(500, function() {
            element1.fadeToggle(500);
        });   
    }    
}

Solution 5

If all you're looking to do is change the text you could do exactly as Kevin has said. But if you're trying to run an animation as well as change the text you could accomplish this by first changing the text then running your animation.

For Example:

$("#test").html('The text has now changed!');
$("#test").animate({left: '100px', top: '100px'},500);

Check out this fiddle for full example:

http://jsfiddle.net/Twig/3krLh/1/

Share:
116,848

Related videos on Youtube

Jeff
Author by

Jeff

I'm a software engineer at Taxfyle, making taxes suck less. I am proficient in JavaScript and C#. http://www.Twitter.com/Jeffijoe

Updated on July 09, 2022

Comments

  • Jeff
    Jeff almost 2 years

    I am trying to animate the html part of a tag ( <font id="test" size="7">This Text!</font> ) using jQuery's Animate function, like so:

    $("#test").delay(1500).animate({text:'The text has now changed!'},500);
    

    However nothing happens, it does not change the text.

    How can I do this? Thanks!

    • JiminP
      JiminP about 13 years
      If you want to change text 'smoothly', you might use two <span>s overlapping and do some work. Or if you just want to change text 'instantly', you can use .text(). PS : You may use <span style=".."> for styling instead of <font ..>.
  • Jeff
    Jeff about 13 years
    I would like the text to change, with the effect that the old text fades out, while the new text fades in :)
  • Rasmus Søborg
    Rasmus Søborg over 11 years
    Great and simple solution. Thank you.
  • Washington Guedes
    Washington Guedes about 8 years
    Actually, you can curry the functions: $(...).html(...).animate(...);
  • lostsoul29
    lostsoul29 over 7 years
    How do you make the old text come back when the cursor hovers away?
  • wpcoder
    wpcoder over 6 years
    again, down voters everywhere, I used this code in production with slight modification. +1. Down-voters: Please test any code before you down vote. offending!
  • Modus Tollens
    Modus Tollens over 6 years
    @wpcoder I did not vote on this answer, but I assume that it was downvoted because it is just showing some code without any explanation. Some users downvote answers without explanation as low quality answer.
  • wpcoder
    wpcoder over 6 years
    The point is; dose this answer help or not, and it helped me , just changed .show(1000) and it worked.
  • Leonardo Sapuy
    Leonardo Sapuy over 6 years
    With a fadeIn-fadeOut instead hide-show seems smoothest, but is a nice solution
  • minhle_r7
    minhle_r7 over 5 years
    why use an external link while it's just few lines of code?