[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/

Reply via email to