using jquery.cloneya plugin hook

Asked

Viewed 181 times

3

I’m using the plugin of jquery.cloneya which is a plugin to clone forms that were already included in a template of an administrative panel I’m using. I don’t quite understand how to use the hook that plugin to increase the name of a input every time he’s cloned.

For example: I click on the "+" sign and clone one or two fields together. These two fields have their name augmented this way > "name='produtos[qtd][2]'", being the index of the first input started with "[qtd][0]".

I asked the developer of plugin if it was possible to clone and increase Names and he answered me the following:

"Yes, it is. Ideally, due to the Nature of cloned form Elements, the Names should be name[], so that the values can be processed as arrays. If you need to increment name and/or value, you can plug into the custom Event - clone_form_input.

See: https://github.com/yapapaya/jquery-cloneya/blob/master/jquery-cloneya.js#L119.

  • Your question is not very clear to me. Is your question how to clone and add [Qtd][] ?? How is your code today?

  • yes, let’s say you have a form, and want to clone a <select> and a text input, and every time you create a clone of these two elements, you want to increment +1 to the input name, if I have an input "products[Qtd][]" = products=>quantity=>(input ID array). it should stay products[Qtd][1], products[Qtd][2]... whenever a new clone is created.

  • your question has not been answered here? http://answall.com/questions/9548/comorclonar-um-elemento-com-jquery-e-addir-um-newname

  • no, this question is not related to this plugin. actually, it was answered but without the use of the plugin, now need using the plugin.

  • How not? It’s there... Jquery Cloneya

  • yes, but the subject answered with pure js. this question is reserved to the plugin.

Show 1 more comment

1 answer

1

Unfortunately the plugin documentation is quite weak, making it very difficult to use.

Anyway I created an example of how to create a form with pure Javascript. I think you will have to end up accepting this solution even.

HTML

<button id='botao'>
    Adicione um formulário
</button>

<div id='formulario'>
    <form id='form' action='#'>
        <input type='submit' id='submit'></input>
    </form>
</div>

Javascript

var sequencia = 0;

var form = document.getElementById('form');
var submit = document.getElementById('submit');

var cria_formulario = function() {

    var input = document.createElement('input');
    input.setAttribute('type', 'text');
    input.setAttribute('name', 'entrada[' + sequencia + ']');
    input.setAttribute('value', 'entrada[' + sequencia + ']');
    sequencia += 1;

    form.removeChild(submit);
    form.appendChild(input);
    form.appendChild(document.createElement('br'));
    form.appendChild(submit);
};

var botao = document.getElementById('botao');
botao.onclick = function() {
    cria_formulario();
}

Jsfiddle

See on Jsfiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.