javascript - remove part of string from textarea with click handler -


i have javascript adds product title textarea when user clicks on "add basket"-button.

<a href="#" class="add-button" data-id="@fieldset.id" data-producttitle="@fieldset.getvalue("productitemtitle")">tilføj</a> 

and javascript

var producttitle = settings.producttitle;            $(".tilvalg textarea").text($(".tilvalg textarea").text() + producttitle + "\n"); 

how can remove line textarea if user click on remove button specific line?

the textarea this:

product name 1 product name 2 product name 3 

if user have added 3 products

if user clicks on "remove" on product 2, textarea should like

product name 1 product name 3 

i not quite sure how achieve this.

it advantageous change javascript use array store strings (and maybe sort them), , can modify list before outputting it. so:

var items = [];  // on click adding     items.push(producttitle); // add item end of array     $(".tilvalg textarea").text(items.join('\n')); // join items new line , output  // on click removing     var index = items.indexof(productnametofind); // find item in array     if(index != -1)         items.splice(index, index); // remove item @ found index     $(".tilvalg textarea").text(items.join('\n')); // join items new line , output 

Comments