简体   繁体   中英

Animating an expanding textarea with jQuery

Objective

I want to animate all of my textareas in various forms. Currently I can only animate one because I am using an ID selector.

Javascript

$(function() {
    $("#content").focus(function(){
        $(this).animate({"height": "85px",}, "fast" );
        $("#button_block").slideDown("fast");
        return false;
    });

    $("#cancel").click(function(){
        $("#content").animate({"height": "30px",}, "fast" );
        $("#button_block").slideUp("fast");
        return false;
    });
});

HTML

<form method="post" action="">
    <textarea  id="content"></textarea>
    <div id="button_block">
        <input type="submit" id="button" value=" Share "/>
        <input type="submit" id='cancel' value=" cancel" />
    </div>
</form>

Try this.Use class and select the closest textarea to animate. A sample demo

 $(function() { $(".content").focus(function() { $(this).animate({ "height": "85px", }, "fast"); $(this).closest('form').find(".button_block").slideDown("fast"); return false; }); $(".cancel").click(function(event) { $(this).closest('form').find('.content').animate({ "height": "30px", }, "fast"); $(this).closest('form').find(".button_block").slideUp("fast"); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" class="button" value=" Share " /> <input type="submit" class='cancel' value=" cancel" /> </div> </form> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" class="button" value=" Share " /> <input type="submit" class='cancel' value=" cancel" /> </div> </form> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" class="button" value=" Share " /> <input type="submit" class='cancel' value=" cancel" /> </div> </form> 

Here you go with a solution https://jsfiddle.net/d71g96cx/

 $(function() { $(".content").focus(function(){ $(this).animate({"height": "85px",}, "fast" ) .siblings('div.button_block') .slideDown("fast"); return false; }); $('input[value="Cancel"]').click(function(){ $(this).parent('div.button_block') .slideUp() .siblings('textarea.content') .animate({"height": "30px"}, "fast" ); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" id="button" value="Share "/> <input type="submit" id='cancel' value="Cancel" /> </div> </form> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" id="button" value="Share "/> <input type="submit" id='cancel' value="Cancel" /> </div> </form> <form method="post" action=""> <textarea class="content"></textarea> <div class="button_block"> <input type="submit" id="button" value="Share "/> <input type="submit" id='cancel' value="Cancel" /> </div> </form> 

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM