Dynamically add and remove elements with jQuery
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Add More Elements</title> <script src="jquery-1.7.1.min.js"></script> <script> $(document).ready (function () { $('.btnAdd').click (function () { $('.buttons').append ( '<input type="text" name="txt"> <input type="button" class="btnRemove" value="Remove"><br>' ); // end append $('.btnRemove').on('click', function () { $(this).prev().remove (); // remove the textbox $(this).next ().remove (); // remove the <br> $(this).remove (); // remove the button }); }); // end click }); // end ready </script> </head> <body> <div class="buttons"> <input type="text" name="txt"> <input type="button" class="btnAdd" value="Add"><br> </div> </body> </html>
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.