wbraganca/yii2-dynamicform change textfield value based on button


I have problem with change value in textfield by button setDefault.

After I clicked on this button I cant access and change value in the name textfield above the button on the "Martin".

I set id "idN2" for this, but nothing happend.



 <?php DynamicFormWidget::begin([

        'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]

        'widgetBody' => '.container-items', // required: css class selector

        'widgetItem' => '.item', // required: css class

        'limit' => 4, // the maximum times, an element can be cloned (default 999)

        'min' => 0, // 0 or 1 (default 1)

        'insertButton' => '.add-item', // css class

        'deleteButton' => '.remove-item', // css class

        'model' => $modelsAddress[0],

        'formId' => 'dynamic-form',

        'formFields' => [



    ]); ?>

    <div class="panel panel-default">

        <div class="panel-heading">

            <i class="fa fa-envelope"></i> Test

            <button type="button" class="pull-right add-item btn btn-success btn-xs"><i class="fa fa-plus"></i> Add name</button>


            <div class="clearfix"></div>


        <div class="panel-body container-items"><!-- widgetContainer -->

            <?php foreach ($modelsAddress as $index => $modelAddress): ?>

                <div class="item panel panel-default"><!-- widgetBody -->

                    <div class="panel-heading">

                        <span class="panel-title-address">Names: <?= ($index + 1) ?></span>

                        <button type="button" class="pull-right remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>

                        <div class="clearfix"></div>


                    <div class="panel-body">



                            // necessary for update action.

                            if (!$modelAddress->isNewRecord) {

                                echo Html::activeHiddenInput($modelAddress, "[{$index}]id");




                        <?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>"idN2"]) ?>


                         <button type="button" onclick="setDefaultF($(this))" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>



            <?php endforeach; ?>



    <?php DynamicFormWidget::end(); ?>


        function setDefaultF(obj){




', \yii\web\View::POS_HEAD) ?>

You are trying to assign id=‘idN2’ for all your text fields in the form. You cannot. You need to assign unique different ids. Try this:

<?= $form->field($modelAddress, "[{$index}]name")->textInput(['maxlength' => true,'id'=>$index]) ?>

//$index is your variable from for loop

<button type="button" onclick="setDefaultF($index)" class="pull-right setDefault btn btn-success btn-xs"><i class="fa fa-plus"></i> SetDefault</button>

and in jquery do:


function setDefaultF(id){



", \yii\web\View::POS_HEAD) ?>

Thank you for your answer, yes, I am tried assign one id=‘idN2’, because, I wanted only testing access to textfield in dynamic form. I can change value in textfield, which is out of dynamicform, but i Cant change value in textfield, which is in dynamicform.

I tried your advice, but nothing happened. Also I found similar problem on the forum and I tried different access to textfield in dynamicform, but also, nothing happened, when I tried in JQuery this:

$('#modelAddress-' + id+ '-name').val("Martin");

, where ‘id’=>$index, how you wrote.

You cannot assign one id to multiple text fields (first learn about ids in HTML). You need to assign different id to text fields, which means:


so your text field will have id as





so on