jQuery(document).ready(function() { function do_zebra(){ jQuery('.form-table tr:nth-child(4n+3)').css({"background" : "#F1F1F1"}); jQuery('.form-table tr:nth-child(4n+4)').css({"background" : "#F1F1F1"}); } do_zebra(); jQuery('input#condition-add-new').parent().parent().css({"background" : "white"}); jQuery('#condition-add-new').click(function(){ //count rows in a .form-table minus last row with the button var sets = jQuery('.form-table tr input[id$="-code"]').length; var rows = ' '; //jQuery(this).parent().parent().before('' + row1.replace('[0]', '['+sets+']') + '' + '' + row2.replace('[0]', '['+sets+']') + ''); jQuery(this).parent().parent().before(rows.replace(/%id/gi, sets).replace(/%n/gi, sets+1)); do_zebra(); }); });