javascript - Can't get data from dynamically created input fields -
i'm fiddling around js / jquery .. i'm trying create preview based on data entered in form. in 1 of input fields script adds new fields every time press tab (or in other words choose next field). these dynamic fields want retrieve data first 2 fields , put them preview. i've done run function adds number on end of id fields, names contestant_1, contestant_2 etc. based on number of fields opened. want data contestant_1 , _2 entered in boxes in preview. made function waiting keyup event of 1 of these input fields. not able react them after have gained _1 , _2. if hard code in first field named _1, comes in preview. in pictures can see have entered boks1 , boks2 (box1 , 2 norewegian) in first 2 fields, boks1 comes up.
preview http://imgur.com/nci4ln1
this code executed when happens in field
$('#tname').keyup(update_tname); $('#contestant_1').keyup(update_contestant_1); $('#contestant_2').keyup(update_contestant_2);
this function updates last 2 have importance on this.
function update_contestant_1(){ console.log("inne update_contestant_1"); $('#preview').slidedown(); $('#pview_contestant_1').fadein(); // values preview var contestant_1 = $('#contestant_1').val(); $('#display_contestant_1').html(contestant_1); }
input field looks this
<lable for="contestant" class="col-md-2 control-label">contestants:</lable> <div class="input-group input-group-option col-md-4"> <input class="form-control" type="text" id="contestant_" name="option[]" placeholder="write contestant here..." required></input> <span class="input-group-addon input-group-addon-remove"> <span class="glyphicon glyphicon-remove"></span> </span> </div>
and function creates new fields, change id , delete them
$(function () { // variable counting how many contestants fields open var i=0; $(document).on('focus', 'div.form-group-options div.input-group-option:last-child input', function () { var sinputgrouphtml = $(this).parent().html(); // uncomment comments below inherit div classes div on index page. // not in use now, because need offset line input fields below each other // var sinputgroupclasses = $(this).parent().attr('class'); // $(this).parent().parent().append('<div class="' + sinputgroupclasses + '">' + sinputgrouphtml + '</div>'); $(this).parent().parent().append('<div class="input-group input-group-option col-md-4 col-md-offset-2">' + sinputgrouphtml + '</div>'); // adds number @ end of id i++; console.log(i); var newid='contestant_'+i; $(this).attr('id',newid); }); $(document).on('click', 'div.form-group-options .input-group-addon-remove', function () { $(this).parent().remove(); // counts down next contestants field opens, has right number i--; console.log(i); var newid='contestant_'+i; $(this).attr('id',newid); }); });
i greatfull help.
also, hope did right, first post here.
Comments
Post a Comment