0

I have a textarea with select list. My select list add new content on the textarea. But if I begin to write on the textarea and after I add content with my select list, I don't know how can I remove the first content and then putt the new content from my select list.

Html :

<div id="selectmodelediv">
        <select>
         <option value=""></option>
        <option value="1111">test1</option>
        <option value="222">test2</option>
        </select><br />

        <textarea id="targetText" name="targetText" class="champ_tel_txtarea" rows="6" cols="50"></textarea>

      </div>

Jquery :

$('select').change(function () {
$('#targetText').text(''); // I try to clear my textarea before to add content
$('#targetText').append($('#selectmodelediv select').val()) // add new content from select list

});
Bisvan
  • 127
  • 1
  • 11

6 Answers6

2

You can use val() on both sides, as it supports <textarea> elements:

$("#targetText").val($("#selectmodelediv select").val());
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

append is used to add some html to the selected element, what you need to do is -

$('select').change(function () {
$('#targetText').val($("#selectmodelediv select").val());
});
Anil Saini
  • 627
  • 3
  • 17
0

Simply use

$('#targetText').val($('#selectmodelediv select').val()) 

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
0
$('select').change(function () {
    $('#targetText').val($(this).val()) // add new content from select list
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Try:

$('select').change(function () {
    $('#targetText').val($('#targetText').val());
});
codingrose
  • 15,563
  • 11
  • 39
  • 58
0

You should use .val() function to get/set the textarea field. According to this answer val() vs. text() for textarea text() sometimes can do "unexpected" things if you think on it as val().

Try the following:

$('#targetText').val($('#selectmodelediv select').val())
Community
  • 1
  • 1