Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <div class="my-form">
  2. <p class="text-box">
  3. <label for="box1">Name <span class="box-number">1</span></label>
  4. <input type="text" name="on0" value="" id="box1" />
  5. <a class="add-box" href="#">Add More Names</a>
  6. </p>
  7. </div>
  8.  
  9.  
  10.  
  11. jQuery(document).ready(function($) {
  12. var max_fields = 10;
  13. var x = 1;
  14.  
  15. $('.my-form .add-box').click(function() {
  16. var n = $('.text-box').length + 1;
  17.  
  18. if(x < max_fields) {
  19. var box_html = $('<p class="text-box"><label for="box' + n + '">Name <span class="box-number">' + n + '</span></label> <input type="text" name="on' + (n - 1) + '" value="" id="box' + n + '" /> <a href="#" class="remove-box">Remove</a></p>');
  20.  
  21. box_html.hide();
  22. $('.my-form p.text-box:last').after(box_html);
  23. box_html.fadeIn('slow');
  24. x++;
  25. }
  26. });
  27.  
  28. $('.my-form').on('click', '.remove-box', function() {
  29. $(this).parent().css('background-color', '#FF6C6C').fadeOut("slow", function() {
  30. $(this).remove();
  31. $('.text-box').each(function(index) {
  32. var label = $(this).children('label');
  33. label.attr('for', 'box' + (index + 1));
  34. label.children('.box-number').text(index + 1);
  35. $(this).children('input').attr({
  36. name: 'on' + index,
  37. id: 'box' + (index + 1)
  38. });
  39. });
  40. });
  41. return false;
  42. });
  43. });
  44.  
  45. <script id="template" type="text/template">
  46. <p class="text-box">
  47. <label for="box{n}">Name <span class="box-number">{n}</span></label> <input type="text" name="on{-1}" value="" id="box{n}" /> <a href="#" class="remove-box">Remove</a>
  48. </p>
  49. </script>
  50.  
  51. var box_html = $('#template').html().replace(/{n}/g, n).replace(/{-1}/g, n-1);
  52. var $box = $(box_html);
  53.  
  54. var n = $('.text-box').length + 1;
  55. if (n <= max_fields) {
  56.  
  57. var $form = $(this).closest('.my-form');
  58. $('p.text-box:last', $form).after($box);
  59.  
  60. $box.hide().fadeIn('slow');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement