Call a jquery (javascript) function from multiple forms on a single page

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2008
Posts: 73
Reputation: filch is an unknown quantity at this point 
Solved Threads: 1
filch filch is offline Offline
Junior Poster in Training

Call a jquery (javascript) function from multiple forms on a single page

 
0
  #1
Oct 20th, 2009
Hi, I am in a real need to get this going quickly and have run into a bit of a stumbling block. Here is what I am trying to do. I am creating a Paypal Add to Cart page for several products. I have this working for a single button form. The main script is:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. $(function(){
  3. // start a counter for new row IDs
  4. // by setting it to the number
  5. // of existing rows
  6. var newRowNum = 2;
  7.  
  8. // bind a click event to the "Add" link
  9. $('#addnew').click(function(){
  10. // increment the counter
  11. newRowNum += 1;
  12.  
  13. // get the entire "Add" row --
  14. // "this" refers to the clicked element
  15. // and "parent" moves the selection up
  16. // to the parent node in the DOM
  17. var addRow = $(this).parent().parent();
  18.  
  19. // copy the entire row from the DOM
  20. // with "clone"
  21. var newRow = addRow.clone();
  22.  
  23. // set the values of the inputs
  24. // in the "Add" row to empty strings
  25. //$('input', addRow).val('');
  26. //$('name', addRow).val('os' + newRowNum);
  27.  
  28. // replace the HTML for the "Add" link
  29. // with the new row number
  30. $('td:first-child', newRow).html('<input type="hidden" name="on' + newRowNum + '" value="Email Address ' + (newRowNum - 1) + '">Recipient');
  31.  
  32. // insert a remove link in the last cell
  33. $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');
  34.  
  35. // loop through the inputs in the new row
  36. // and update the ID and name attributes
  37. /*$('td:first-child.next-child.input', newRow).each(function(i){
  38. var newID = newRowNum;
  39. $(this).attr('id','os' + newID).attr('name','os' + newID);
  40. });*/
  41. /*$('td:second-child.input', newRow).each(function(i){
  42. var newID = newRowNum;
  43. $(this).attr('id','os' + newRowNum).attr('name','os' + newRowNum);
  44. });*/
  45. $('input:hidden', newRow).attr('id','on' + newRowNum ).attr('name','on' + newRowNum );
  46. $('input:text', newRow).attr('id','os' + newRowNum ).attr('name','os' + newRowNum );
  47.  
  48. //$('td:first-child.next', newRow).html('<input type="text" id="os' + newRowNum + '" name="os' + newRowNum + '" value="">');
  49.  
  50. // insert the new row into the table
  51. // "before" the Add row
  52. addRow.before(newRow);
  53. document.tp01.quantity.value = newRowNum-1;
  54. // add the remove function to the new row
  55. $('a.remove', newRow).click(function(){
  56. $(this).parent().parent().remove();
  57. return false;
  58. });
  59.  
  60. // prevent the default click
  61. return false;
  62. });
  63. });
  64. </script>

This is called from a text link in the page as follows:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="hidden" name="on2" value="Email Address 1"><a id="addnew" href="">Add</a></td><td><input name="os2" type="text" id="os2" value="" size="24" maxlength="60" />

Each successive click on the Add link adds a new row to the table and allows another email address to be added to the form. The trick now is that I need to add multiple Paypal button forms to the page that re-uses this function and perhaps each form calls this function with something like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="hidden" name="on2" value="Email Address 1"><a id="addnew2" href="">Add</a></td><td><input name="os2" type="text" id="os2" value="" size="24" maxlength="60" />
and
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="hidden" name="on2" value="Email Address 1"><a id="addnew3" href="">Add</a></td><td><input name="os2" type="text" id="os2" value="" size="24" maxlength="60" />
etc.

Can anyone guide me as to how to amend the function to accept calls from different links like this so that the new rows are added or removed from the appropriate form on the page? I have tried the following:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. $(function(){
  3. // start a counter for new row IDs
  4. // by setting it to the number
  5. // of existing rows
  6. var newRowNum = 2;
  7. var bindLink = function(linkSelector) {
  8.  
  9. // bind a click event to the "Add" link
  10. $(linkSelector).click(function() {
  11. // increment the counter
  12. newRowNum += 1;
  13.  
  14. // get the entire "Add" row --
  15. // "this" refers to the clicked element
  16. // and "parent" moves the selection up
  17. // to the parent node in the DOM
  18. var addRow = $(this).parent().parent();
  19.  
  20. // copy the entire row from the DOM
  21. // with "clone"
  22. var newRow = addRow.clone();
  23.  
  24. // set the values of the inputs
  25. // in the "Add" row to empty strings
  26. //$('input', addRow).val('');
  27. //$('name', addRow).val('os' + newRowNum);
  28.  
  29. // replace the HTML for the "Add" link
  30. // with the new row number
  31. $('td:first-child', newRow).html('<input type="hidden" name="on' + newRowNum + '" value="Email Address ' + (newRowNum - 1) + '">Recipient');
  32.  
  33. // insert a remove link in the last cell
  34. $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');
  35.  
  36. // loop through the inputs in the new row
  37. // and update the ID and name attributes
  38.  
  39. $('input:hidden', newRow).attr('id','on' + newRowNum ).attr('name','on' + newRowNum );
  40. $('input:text', newRow).attr('id','os' + newRowNum ).attr('name','os' + newRowNum );
  41.  
  42. // insert the new row into the table
  43. // "before" the Add row
  44. addRow.before(newRow);
  45. document.tp01.quantity.value = newRowNum-1;
  46. // add the remove function to the new row
  47. $('a.remove', newRow).click(function(){
  48. $(this).parent().parent().remove();
  49. return false;
  50. });
  51.  
  52. // prevent the default click
  53. return false;
  54. });
  55. };
  56. });
  57. </script>
called with:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="hidden" name="on2" value="Email Address 1">
  2. <a id="addnew2" href="" onclick="bindLink('addnew2')">Add</a>
as suggested by someone else but does not work.

Thanks for any help.

Dave
Reply With Quote Quick reply to this message  
Reply

Tags
javascript, jquery, paypal

Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC