I would like to add the value of an input field to the value of the attribute data-value
. This actually works but I have a button that generates new fields. So the value can be added to the first input field but not to those which get generated by clicking the button.
<button id="another_field">Add a new field</button>
<div class="cpt_area">
<input placeholder="Type something here..." class="full_width cpt_name" type="text" name="cpt_name[]">
</div>
// This will copy an input field
$('#another_field').click(function() {
$('.cpt_name:first').clone().prependTo(".cpt_area").val('');
});
// This will generate a data-value attribute
$('input').keyup(function() {
$(this).attr('data-value', $(this).val());
});
You can try it here: https://jsfiddle.net/e6ybk2d3/
Any ideas how I can fix this?