add select field dynamically (laravel project)

0

I'm creating a form from a repository and I have several fields like that of modality. What I do not know how to do is create a function so that when a person clicks the + button next to the select, it creates on the screen another select field of modality exactly the same as the first one (in this case, the image) and what I can do my loop to feed it with the bank data.

I'm a front end door (I believe it's that)

    
asked by anonymous 24.04.2018 / 01:26

1 answer

0

Use the function .clone() of JQuery , create a select with the default options without id and hidden to serve as the base, so when you click on the add it copies this select and displays on the screen, already to popular with bank data is the same function just put it inside a loop to receive the data.

JS

jQuery('select[name="default"]').hide();
jQuery('#addSelect').on('click', function() {
    var novoSelect = jQuery('select[name="default"]').clone();
    novoSelect.attr('name', 'select1')
    novoSelect.show();
    jQuery('body').append(novoSelect);
});

Html

<select name="default">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>
<button id="addSelect">add</button>
    
24.04.2018 / 14:53