Skip to content

Instantly share code, notes, and snippets.

@kprimdal-dk
Created February 27, 2013 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kprimdal-dk/5047641 to your computer and use it in GitHub Desktop.
Save kprimdal-dk/5047641 to your computer and use it in GitHub Desktop.
WordPress Meta field repeater - ( taken from Easy Digital Download )
jQuery(document).ready(function ($) {
var mux_repeater_configuation = {
init: function() {
this.add();
this.remove();
},
add: function() {
$('body').on('click', '.mux_ps_add_repeatable', function(e) {
e.preventDefault();
var button = $( this ),
row = button.parent().parent().prev('tr'),
clone = mux_slider_configuation.new_clone( row );
clone.insertAfter( row );
});
},
new_clone: function( row ) {
var clone = row.clone(),
id = parseInt( clone.attr('id') ) + 1;
clone.attr('id', id);
clone.find( 'td input' ).val( '' );
clone.find( 'input' ).each(function() {
var name = $( this ).attr( 'name' );
name = name.replace( /\[(\d+)\]/, '[' + id + ']');
$( this ).attr( 'name', name ).attr( 'id', name );
});
return clone;
},
remove: function() {
$('body').on('click', '.mux_ps_remove_repeatable', function(e) {
e.preventDefault();
console.log('test');
var row = $(this).parent().parent('tr'),
count = row.parent().find('tr').length - 1;
if ( count > 1 ) {
row.fadeOut(400, function( element ) {
$(this).remove();
});
}
});
}
};
mux_repeater_configuation.init();
});
<table class="widefat" width="90%" cellpadding="0" cellpadding="0">
<tr id="0">
<td>
<input type="text" name="prices[0][amount]" placeholder="<?php echo _e('Amount', 'mux_ps') ?>" style="width:100%;">
</td>
<td>
<input type="text" name="prices[0][months]" placeholder="<?php echo _e('Months', 'mux_ps') ?>" style="width:100%;">
</td>
<td>
<input type="text" name="prices[0][interest]" placeholder="<?php echo _e('Interest', 'mux_ps') ?>" style="width:100%;">
</td>
<td>
<a class="mux_ps_remove_repeatable">×</a>
</td>
</tr>
<tr>
<td class="submit" colspan="4">
<a class="button-secondary mux_ps_add_repeatable"><?php _e( 'New', 'mux_ps' ) ?></a>
</td>
</tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment