tags:

views:

48

answers:

1

Hi,

I have the following function:

<script type="text/javascript">
     $(function(){
      // start a counter for new row IDs
      // by setting it to the number
      // of existing rows
      var newRowNum = 2;

      // bind a click event to the "Add" link
      $('#addnew').click(function() {
       // increment the counter
       newRowNum += 1;

       // get the entire "Add" row --
       // "this" refers to the clicked element
       // and "parent" moves the selection up
       // to the parent node in the DOM
       var addRow = $(this).parent().parent();

       // copy the entire row from the DOM
       // with "clone"
       var newRow = addRow.clone();

       // set the values of the inputs
       // in the "Add" row to empty strings
       //$('input', addRow).val('');
       //$('name', addRow).val('os' + newRowNum);

       // replace the HTML for the "Add" link 
       // with the new row number
       $('td:first-child', newRow).html('<input type="hidden" name="on' + newRowNum + '" value="Email Address ' + (newRowNum - 1) + '">Recipient');

       // insert a remove link in the last cell
       $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');

       // loop through the inputs in the new row
       // and update the ID and name attributes

       $('input:hidden', newRow).attr('id','on' + newRowNum ).attr('name','on' + newRowNum );
       $('input:text', newRow).attr('id','os' + newRowNum ).attr('name','os' + newRowNum );

       // insert the new row into the table
       // "before" the Add row
       addRow.before(newRow);
       document.tp01.quantity.value = newRowNum-1;
       // add the remove function to the new row
       $('a.remove', newRow).click(function(){
        $(this).parent().parent().remove();
        return false;    
       });

       // prevent the default click
       return false;
      });
     });

     </script>

This function is called by clicking on a link in a form (this function adds or removes rows from a table). The link looks like this:

<a id="addnew" href="">Add</a>

I need to put more forms on the same page accessed by a link in each of those forms that is, as far as the user is concerned, exactly the same as the one shown above. Can someone make suggestions as to how I can reuse the same function to accomplish this?

Thanks

Dave

A: 

move the embedded script to an external JS file:

var newRowNum = 2;

var bindLink = function(linkSelector)
{
    // bind a click event to the "Add" link
    $(linkSelector).click(function() {
        ...
    }
}

Then in place of the embedded script put a call to bindLink('#addnew') in your ready handler.

Frank Schwieterman
So, to be clear about this, I move the current script from embedded to external ( I was going to do this eventually but, it should not make any difference if it is embedded or external, should it?). I then put the above code in the external JS script as well? Or do I add what you have above to the current script?Dave
Dave
I would embed the call to bindLink(), as the id ('#id') passed for each link would be different. I suppose you could give every link the same id, then you could move the bindLink call to the external script as well.
Frank Schwieterman
Also to clarify: the old function was called with a simple <a id="addnew" href="">Add</a> Do I now need to do an onclick="javascript ..." or can I call it with <a id="addnew" href="">Add</a> and then <a id="addnew2" href="">Add</a> etc?Thanks for your help.Dave
Dave
I am still trying to get this going. I wonder, can you clarify what you mean by ready handler. I am not a jquery expert by any means so I am not familiar with this term. I am sorry but I really do not understand what you are saying I need to do here. If I move the currently embedded script to an external file ( I am not too sure why moving it to an external file makes a difference here), I need to pass a call to this function from various forms on the page via what: an onclick="bindLink('addNew2')" type of call?Dave
Dave
By ready handler, I mean: $.ready(function() {...}) or $(function() {...}))These allow you to have function() {...} run when the document is finish loading. Your original/big code snippet has everything within a ready handler.
Frank Schwieterman