1

I have a function:

<?php $max = 5; ?>

<script type="text/javascript">
jQuery(function($) {
    var scntDiv = $('#div');
    var i = $('#div p').size() + 2;
    var x = <?php echo $max;?>;
    var a = 0;
    $('#add').click (function() {
        $('<p><input type="text" id="" name="field['+a+'][0]" value="" placeholder="" /><a href="#" id="delete">Remove</a></p>').appendTo(scntDiv);
        i++;
        a++;
        return false;
    });
    $('#delete').live('click',function() {
        if( i > 2 ) {
        $(this).parents('p').remove();
        i--;
        }
        return false;
    });
});
</script>

As you can see var x is constant and var a iterates in every click of add button. How to make these two variables sum each other with every iteration and to replace +a+ in name="field['+a+'][0]"?

Fisch
  • 3,775
  • 1
  • 27
  • 38
Daniel
  • 538
  • 1
  • 8
  • 21
  • I'm not sure if I understand you, but, why don't you init `var a = x;` ? (instead `var a = 0;`) – Roberto Mar 21 '14 at 18:16
  • What do you mean by "sum each other"? Something like `var b = a + x;`? – Julio Mar 21 '14 at 18:16
  • @Roberto Sánchez - you're right! I don't know why I didn't think about it, I must have a brainstorm :/ Thank you!! BTW, How to close this question as solved? – Daniel Mar 21 '14 at 18:21

1 Answers1

3

So, to extract it from comments, the easiest solution in this case would be:

Change:

var a = 0;

By

var a = x;
Roberto
  • 8,586
  • 3
  • 42
  • 53