wmarx
wmarx

Reputation: 27

How to add multiple input fileds to a div using jquerys clone and append

I am using jquery, to add dynamically two input fields in a new "row" div. The problem is, that only the second input field is cloned and appended.I can´t find the solution. Any help is appreciated.

Trainer= {
    neuen_trainer_hinzufuegen(){
        var n = $( ".trainerposition" ).length;
         $( "#trainer" ).append( $('<div class="trainerposition" id="trainerposition_'+n+'"></div>') );

         $( "#trainer_0_vorname" ).clone().appendTo( "#rtrainerposition_"+n );
         $( "#trainerposition_"+n+" input:nth-child(2)").attr('name', 'trainer_'+n+'_vorname');
         $( "#trainerposition_"+n+" input:nth-child(2)").attr('id', 'trainer_'+n+'_vorname');

         $( "#trainer_0_nachname" ).clone().appendTo( "#trainerposition_"+n );
         $( "#trainerposition_"+n+" input:nth-child(3)").attr('name', 'trainer_'+n+'_nachname');    
         $( "#trainerposition_"+n+" input:nth-child(3)").attr('id', 'trainer_'+n+'_nachname');

    }

}

Upvotes: 0

Views: 31

Answers (1)

Pethical
Pethical

Reputation: 1482

You have an extra r in your selector :-)

$( "#trainer_0_vorname" ).clone().appendTo( "#rtrainerposition_"+n );

I think this should be

$( "#trainer_0_vorname" ).clone().appendTo( "#trainerposition_"+n );

Upvotes: 1

Related Questions