Thanks Charles, that code helps a lot. From reading it over I understand how it works, However, it's giving me one parse error on the line with a single double quote. Is it possible to combine the 2 lines below or escape the quote somehow... I'm not familiar with escaping javascript like in php.
--- Code --- extrafield += ' value="<?php echo $listcreate->' + padded_counter; extrafield += '->EditValue ?>"></td></tr>'; --- Code --- Thanks. On Jan 27, 8:09 am, "Charles K. Clarkson" <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > : Is it possible to auto increment the value on a variable that will be > : appended? > > Yes. We would need to use a closure to preserve the value of the > counter between clicks. The function in the $(document).ready section > can act as the closure. > > : For example the bottom code I'm appending an extra row with a text > : field to the table once the fields button is clicked. I want the > " data for every "c#" sign to count up from 1, 2, 3, etc... Any ideas > : appreciated, thanks. > : > : Further Note: If the auto increment is possible, could I do it as 001, > : 002, 003... Thanks. > > I included a function to do that. It relies on adding a repeat > method to the built-in String object. > > : --- Code --- > > (function($) { > > // Add repeat method to String object > String.prototype.repeat = function(n){ > return new Array(n + 1).join(this); > }; > > // Add leading_zeros method to jQuery > $.leading_zeros = function(n, total_digits){ > n = n.toString(); > return '0'.repeat(total_digits - n.length) + n; > }; > > })(jQuery); > > $(document).ready(function(){ > > // initialize the counter > var counter = 0; > > $('#fields-button').click(function(){ > > // increment and pad the counter > var padded_counter = $.leading_zeros(++counter, 3); > > // create the new field > var extrafield = '<tr><td>List Item ' + padded_counter + > '</td><td>'; > extrafield += '<input type="text" name="' + padded_counter + > '"' > extrafield += ' id="' + padded_counter + '"' > extrafield += ' title="" size="30" maxlength="255"' > extrafield += ' value="<?php echo $listcreate->' + > padded_counter; > extrafield += '->EditValue ?>"></td></tr>'; > > // add the new field to the table > $('#fields').append(extrafield); > }); > }); > > : --- Code --- > > <body> > <button id="fields-button">Add Field</button> > <table id="fields"></table> > </body> > > HTH, > > Charles K. Clarkson > -- > Mobile Homes Specialist > Free Market Advocate > Web Programmer > > 254 968-8328 > > http://www.clarksonenergyhomes.com/wordpress/about/