Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. $(document).ready(function () {
  2. $('#btnAdd').click(function () {
  3. var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
  4. var newNum = new Number(num + 1); // the numeric ID of the new input field being added
  5.  
  6. // create the new element via clone(), and manipulate it's ID using newNum value
  7. var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
  8.  
  9. // manipulate the name/id values of the input inside the new element
  10. newElem.children(':first').attr('id', 'author' + newNum).attr('name', 'author' + newNum).attr('value', 'enter a name');
  11.  
  12. // insert the new element after the last "duplicatable" input field
  13. $('#input' + num).after(newElem);
  14.  
  15. // enable the "remove" button
  16. $('#btnDel').attr('disabled', '');
  17.  
  18. // business rule: you can only add 5 names
  19. if (newNum == 5)
  20. $('#btnAdd').attr('disabled', 'disabled');
  21. });
  22.  
  23. $('#btnDel').click(function () {
  24. var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
  25. $('#input' + num).remove(); // remove the last element
  26.  
  27. // enable the "add" button
  28. $('#btnAdd').attr('disabled', '');
  29.  
  30. // if only one element remains, disable the "remove" button
  31. if (num - 1 == 1)
  32. $('#btnDel').attr('disabled', 'disabled');
  33. });
  34.  
  35. $('#btnDel').attr('disabled', 'disabled');
  36. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement