charlie charlie - 4 months ago 12
HTML Question

Prepend value to textarea

I have created this JQuery code to prepend a textarea input

$(document).ready(function() {
var data = <?php echo $data; ?>;

$('#standard_response').on('change', function() {
var sequence = $(this).val();
//Check each object in the data array
$.each(data, function( index, obj ) {
if(obj.sequence === sequence) {
$('#description').prepend( obj.response );
}
});
//$('#standard_response').select2('val', '');
});
});


but it only works when the textarea is empty.

i want it to add the text at the end of the textarea where the cursor was left

i have also tried:

$('#description').text( $('#description').text() + obj.response );

$('#description').text( $('#description').val() + obj.response );


but neither of these work either

Answer

Use val

var value = $('#description').val();
$('#description').val(value + obj.response );