pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

jeremykendall private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Jeremy Kendall on Tue 20 Jan 17:10
report abuse | download | new post

  1. <!-- dynamic-form-elements.phtml -->
  2.  
  3. <p>All fields are required.</p>
  4.  
  5. <?= $this->form; ?>
  6.  
  7. <script type="text/javascript">
  8.  
  9. $(document).ready(function() {
  10.  
  11.   $("#addElement").click(
  12.       function() {
  13.           ajaxAddField();
  14.        }
  15.     );
  16.  
  17.   $("#removeElement").click(
  18.       function() {
  19.           removeField();
  20.       }
  21.     );
  22.   }
  23. );
  24.  
  25. // Get value of id - integer appended to dynamic form field names and ids
  26. var id = $("#id").val();
  27.  
  28. // Retrieve new element's html from controller
  29. function ajaxAddField() {
  30.   $.ajax(
  31.     {
  32.       type: "POST",
  33.       url: "<?=$this->url(array('action' => 'newfield', 'format' => 'html'));?>",
  34.       data: "id=" + id,
  35.       success: function(newElement) {
  36.        
  37.         // Insert new element before the Add button
  38.         $("#addElement-label").before(newElement);
  39.        
  40.         // Increment and store id
  41.         $("#id").val(++id);
  42.       }
  43.     }
  44.   );
  45. }
  46.  
  47. function removeField() {
  48.  
  49.   // Get the last used id
  50.   var lastId = $("#id").val() - 1;
  51.  
  52.   // Build the attribute search string.  This will match the last added  dt and dd elements.  
  53.   // Specifically, it matches any element where the id begins with 'newName<int>-'.
  54.   searchString = '*[id^=newName' + lastId + '-]';
  55.  
  56.   // Remove the elements that match the search string.
  57.   $(searchString).remove()
  58.  
  59.   // Decrement and store id
  60.   $("#id").val(--id);
  61. }
  62.  
  63. </script>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post