Nikk
Nikk

Reputation: 7891

Animating inserted elements with jQuery

How do I add animations (both in and out) for the tooltips here:

$('.requiredField').each(function () {
    if ($.trim($(this).val()) == '') {
        var labelText = $(this).prev('label').text();
        $(this).parent().append('<div class="error">Please enter your ' + labelText + '!</div>');
        $(this).addClass('inputError');
        hasError = true;
    } else if ($(this).hasClass('email')) {
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
        if (!emailReg.test($.trim($(this).val()))) {
            var labelText = $(this).prev('label').text();
            $(this).parent().append('<div class="error">Sorry! You\'ve entered an invalid ' + labelText + '.</div>');
            $(this).addClass('inputError');
            hasError = true;
        }
    }
}

Also, how do I add animation to this banner that pops out, once the form has been submitted? Notice, the form has a fadeOut animation. I would also like to animate the paragraph with class=info:

var formInput = $(this).serialize();
$.post($(this).attr('action'), formInput, function (data) {
    $('form#contact-us').fadeOut("fast", function () {
        $(this).before('<p class="info">Thanks! Your email has been submitted. Huzzah!</p>');
    })
});

Script: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

<script type="text/javascript">
    <!--//--><![CDATA[//><!--
    $(document).ready(function() {
        $('form#contact-us').submit(function() {
            $('form#contact-us .error').remove();
            var hasError = false;
            $('.requiredField').each(function() {
                if($.trim($(this).val()) == '') {
                    var labelText = $(this).prev('label').text();
                    $(this).parent().append('<div class="error">Please enter your '+labelText+'!</div>');
                    $(this).addClass('inputError');
                    hasError = true;
                } else if($(this).hasClass('email')) {
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
                    if(!emailReg.test($.trim($(this).val()))) {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<div class="error">Sorry! You\'ve entered an invalid '+labelText+'.</div>');
                        $(this).addClass('inputError');
                        hasError = true;
                    }
                }
            });
            if(!hasError) {
                var formInput = $(this).serialize();
                $.post($(this).attr('action'),formInput, function(data){
                    $('form#contact-us').fadeOut("fast", function() {                  
                        $(this).before('<p class="info">Thanks! Your email has been submitted. Huzzah!</p>');
                    });
                });
            }

            return false;   
        });
    });
    //-->!]]>
</script>

Upvotes: 0

Views: 3333

Answers (2)

lukeocom
lukeocom

Reputation: 3243

The easiest way to animate the div's you are appending, is to have them at 0 width or height, or display:none, and then once you append them, then animate. A simple example is here - http://jsfiddle.net/Bquyw/1

which goes something like:

$('div').append('<div class="error">Please enter your !</div>');
$('.error').animate({'width':'200px'},1000,'easeOutBounce')

You may be interested in this codePen too - http://codepen.io/lukeocom/pen/Dewdo

Upvotes: 1

MasterAM
MasterAM

Reputation: 16488

In order to animate an appended element, you can simply go the other way around.

Instead of:

someElement.append('<div>myElement</div>');

do:

$('<div>myElement</div>').hide().appendTo(someElement).show('slow');

and if you still want to use append() or before(), you can have the new element assigned to a variable, as follows:

Original:

$(this).before('<p class="info">Thanks! ... Huzzah!</p>');

Modified:

var el = $('<p class="info">Thanks! ... Huzzah!</p>').hide();
$(this).before(el);
el.show('slow');

This method should work for all of your instances. You can change show() with your desired effect, such as fadeIn().

Demo.

Upvotes: 4

Related Questions