Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. +-
  2. Argentina | San Juan | Rawson
  3. Chile | Santiago | Chiñihue
  4.  
  5. Chile | Santiago | Chiñihue
  6.  
  7. // Version 1
  8. $(function() {
  9.  
  10. // Iterator for the dupe ids
  11. var i = 0;
  12.  
  13. $('#clone').click(function() {
  14. // put the clone() into cloned
  15. var cloned = $('#template').clone();
  16.  
  17. // Add .dupe class to cloned
  18. $(cloned).addClass('dupe');
  19.  
  20. // Set the id of cloned, use i++ instead of incrementing it elsewhere.
  21. $(cloned).attr('id', 'duplicate'+ i++);
  22.  
  23. // Append cloned to #filter
  24. $(cloned).appendTo('#filter');
  25.  
  26. // Passing selector rather than iteration
  27. chainItWithId($(cloned));
  28.  
  29. // If this is NOT the first addition, do some kludge
  30. if ($('#filter div.dupe').length!==1) {
  31. // Set the previous clone to lastClone
  32. var lastClone = $(cloned).siblings('div.dupe:last');
  33.  
  34. // Set the value of .pais to the value of the previous .pais
  35. $(cloned).find('.pais').val($(lastClone).find('.pais').val());
  36. // Do the "change" event manually.
  37. $(cloned).find('.pais').change();
  38.  
  39. // Set the value of .provincia to the value of the previous .provincia
  40. $(cloned).find('.provincia').val($(lastClone).find('.provincia').val());
  41. // Do the "change" event manually
  42. $(cloned).find('.provincia').change();
  43.  
  44. // Set the value of .ciudad to the value of the previous .cudad
  45. $(cloned).find('.ciudad').val($(lastClone).find('.ciudad').val());
  46.  
  47. }
  48.  
  49. // Show the hidden div
  50. $('#filter div:hidden').show();
  51.  
  52. });
  53. $('#remove').click(function() {
  54. // Remove all but the very last set of options
  55. if ($('#filter > div').length > 1) {
  56. $('#filter > div').last().remove();
  57. }
  58. });
  59.  
  60. // Manually do the "click" event
  61. $('#clone').click();
  62. });
  63.  
  64. // Here I'm getting the cloned full selector
  65. function chainItWithId(cloned) {
  66. // Chain .provincia to .pais in the current clone
  67. $(cloned).find('.provincia').chained($(cloned).find('.pais'));
  68. // Chain .ciudad to .provincia in the current clone
  69. $(cloned).find('.ciudad').chained($(cloned).find('.provincia'));
  70. }
  71.  
  72. // Version 2
  73. $(function() {
  74.  
  75. $('#clone').click(function() {
  76. // put the clone() into cloned
  77. var cloned = $('#template').clone();
  78.  
  79. // Add .dupe class to cloned
  80. $(cloned).addClass('dupe');
  81.  
  82. // Set the id to the count of div.dupe elements in #filter
  83. // This will increment 0,1,2,3 as you add elements.
  84. $(cloned).attr('id', 'duplicate'+ $('#filter div.dupe').length);
  85.  
  86. // Append cloned to #filter
  87. $(cloned).appendTo('#filter');
  88.  
  89. // Passing selector rather than iteration
  90. chainItWithId($(cloned));
  91.  
  92. // If this is NOT the first addition, do some kludge
  93. if ($('#filter div.dupe').length!==1) {
  94. // Set the previous clone to lastClone
  95. var lastClone = $(cloned).siblings('div.dupe:last');
  96.  
  97. // Set the value of .pais to the value of the previous .pais
  98. $(cloned).find('.pais').val($(lastClone).find('.pais').val());
  99. // Do the "change" event manually.
  100. $(cloned).find('.pais').change();
  101.  
  102. // Set the value of .provincia to the value of the previous .provincia
  103. $(cloned).find('.provincia').val($(lastClone).find('.provincia').val());
  104. // Do the "change" event manually
  105. $(cloned).find('.provincia').change();
  106.  
  107. // Set the value of .ciudad to the value of the previous .cudad
  108. $(cloned).find('.ciudad').val($(lastClone).find('.ciudad').val());
  109.  
  110. }
  111.  
  112. // Show the hidden div
  113. $('#filter div:hidden').show();
  114.  
  115. });
  116. $('#remove').click(function() {
  117. // Remove all but the very last set of options
  118. if ($('#filter > div').length > 1) {
  119. $('#filter > div').last().remove();
  120. }
  121. });
  122.  
  123. // Manually do the "click" event
  124. $('#clone').click();
  125. });
  126.  
  127. // Here I'm getting the cloned full selector
  128. function chainItWithId(cloned) {
  129. // Chain .provincia to .pais in the current clone
  130. $(cloned).find('.provincia').chained($(cloned).find('.pais'));
  131. // Chain .ciudad to .provincia in the current clone
  132. $(cloned).find('.ciudad').chained($(cloned).find('.provincia'));
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement