I have script, that on press of Enter on keyboard hides $this input and generates span with value of that field to display.
$('.work_experience .achievements').on('keypress','input', function(event){
if (!$(this).val() && event.which == 13 ) {
alert('Input something');
event.preventDefault();
}
else if (event.which == 13) {
event.preventDefault();
var value = $(this).val();
$(this).parent().append('<span><i class="fa fa-times"></i> ' + value + '</span><input type="text" class="form-control input-xs" placeholder="I did" autofocus>');
$(this).css('display', 'none');
$('.achievements input:last-child').focus();
}
});
It works. But then I have button, clicking on which generates new input field. And on that, newly created input, script doesn't work anymore. Help is appreciated.
PS Script adding new set of fields
$('.form_fields').on('click','#add_work', function(event){
event.preventDefault();
var field_set = '<div class="form-group work_experience">'+
'<input type="text" class="form-control input-m" id="company_name" placeholder="Company name">'+
'<input type="text" class="form-control input-m" id="company_place" placeholder="Company location">'+
'<input type="text" class="form-control input-m" id="work_period_start" placeholder="Beginnig">'+
'<input type="text" class="form-control input-m" id="work_period_end" placeholder="End">'+
'<label><input type="checkbox"> Currently</label>'+
'<textarea class="form-control" rows="5" placeholder="Write a few sentences about that company"></textarea>'+
'<input type="text" class="form-control input-m" placeholder="Your position">'+
'<label><strong>Achievements</strong></label>'+
'<div class="achievements">'+
'<input type="text" class="form-control input-xs" placeholder="I did">'+
'</div>'+
'</div>';
$(this).prev().after(field_set);
});
$(this).parent(), but the delegated handlers are attached to$(this)and not to its parent. therefore the newly added inputs are placed outside of the handler's monitored scope