Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Copy / move text from select list to textarea

The img below describes what I am trying to do:

Copy text from select list to textarea

So when an entry from the select list is selected and button "Copy" is pressed, it will add an < li > element to the text area.

Any ideas, resources?

like image 980
Valentin Despa Avatar asked Feb 20 '23 04:02

Valentin Despa


1 Answers

I give you an example, simple one:

HTML code:

<select multiple="multiple" class="options">
    <option value="item1">Item 1</option>
    <option value="item2">Item 2</option>
    <option value="item3">Item 3</option>
    <option value="item4">Item 4</option>
    <option value="item5">Item 5</option>
</select>


<button id="test">Copy</button>

<textarea cols="25" rows="5" id="textarea"></textarea>

Javascript:

$(function(){
    $("#test").on("click", function(){
        $("#textarea").empty(); //to empty textarea content
        $(".options option:selected").each(function(){
           $("#textarea").append("* "+$(this).text()+ "\n");
        }); 
    });       
});

Demo: http://jsfiddle.net/pf5CU/

Update

http://jsfiddle.net/pf5CU/1/

like image 102
Snake Eyes Avatar answered Feb 21 '23 17:02

Snake Eyes