Add X meta box inputs based on form at top of meta box, how to do it right?

You could clone an existing field with jQuery and append it to the form. You’ll also have to handle cases where the number of ingredients is fewer than the number of fields, and you’ll have to use jQuery’s remove. Here’s a simple example of cloning a field:

$('#quantity-form').submit(function(){
    // get # of fields that already exist
    // (change this to match whatever type of fields you have)
    var count = $('#metabox-form input[type="text"]').length;
    // you'll want to check if count is > or < requested quantity and possibly remove,
    // or clone (in a loop to add multiple fields):
    $('#metabox-form input[type="text"]:last')
        .clone(false)
        .attr({
            name:'field-' + (count + 1),
            id:'field-' + (count + 1)
        })
        .appendTo('#metabox-form');

    return false;                   
});

Leave a Comment