Update TextArea value based on number of listbox selections

Grizzly

If I have a listbox and a textarea:

<textarea id="MyTextArea"></textarea>

<select id="SelectList" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

What I would like to do is... if there are more than 0 selected options.. then append the text There are selections to the textarea.

If the user deselects the options to the point where there are 0 selected options then erase that string from the textarea, but keep whatever else the user typed in that textarea.

Here is what I have:

$("#SelectList").change(function(){
    var count = $("#SelectList> option:selected").length;
    var string = "There are selections.";
    var txtVal = $("#MyTextArea").val();

    if (count > 0 && txtVal.indexOf(string) == -1) {
        $("#MyTextArea").append(string);
    }
    else if (count > 0 && txtVal.indexOf(string) != -1) {
    }
    else if (count === 0 && txtVal.indexOf(string) != -1) {
        var indexOfString = $("#MyTextArea").value.indexOf(string);
        $("#MyTextArea").value.substring(0, indexOfString - 1);
    }
});

This works for adding the string only once. But when I deselect all of the options so that there are 0 selections.. I get this:

Unable to get property 'indexOf' of undefined or null reference

On this line: var indexOfString = $("#MyTextArea").value.indexOf(string);

Any help is appreciated.

Lorav

It should be :

var indexOfString = $("#MyTextArea").val().indexOf(string);

Because $("#MyTextArea") is a jQuery element

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to limit the number of selections in a tkinter listbox?

From Dev

Limit number of selections in a multiselect ListBox in Excel

From Dev

How to limit the number of selections in a tkinter listbox?

From Dev

Changes in other elements based on listbox selections in threepenny-gui

From Dev

Update form fields based on checkbox selections

From Dev

Reducing count value in span based on number of characters entered in textarea

From Dev

Limit number of checkboxes and update string based on their value

From Dev

Listbox Sorting based on TextBox value

From Dev

Update one form based on selections from another form

From Dev

VBA Userform Populate textbox with cell value based on 2 combobox selections

From Dev

Update based on Row number

From Dev

Update based on Row number

From Dev

how to use the listbox value to update textbox

From Dev

Delete a file based on the value of the SelectedItem in a ListBox

From Dev

Insert update based on value

From Dev

Vaadin 7 - update textarea at value changed

From Dev

Protractor to get value of textarea with line number

From Dev

Excel VBA: Searching for value in listbox based on value set in textbox

From Dev

Excel VBA: Searching for value in listbox based on value set in textbox

From Dev

How can I update a wxPython ListBox based on some search string?

From Dev

SQL - Need to update a value in a table based the number of relations its row has to another table

From Dev

Update row based on conditional value

From Java

Write value based on number of rows

From Dev

Sort array based on number in value

From Dev

Count value based on name and number

From Dev

Update value based on the previous updated value mysql

From Dev

Jq update JSON key:value based on value

From Dev

Update checkbox and textarea value in mysql db using Jquery

From Dev

Update a column based on row_number()

Related Related

  1. 1

    How to limit the number of selections in a tkinter listbox?

  2. 2

    Limit number of selections in a multiselect ListBox in Excel

  3. 3

    How to limit the number of selections in a tkinter listbox?

  4. 4

    Changes in other elements based on listbox selections in threepenny-gui

  5. 5

    Update form fields based on checkbox selections

  6. 6

    Reducing count value in span based on number of characters entered in textarea

  7. 7

    Limit number of checkboxes and update string based on their value

  8. 8

    Listbox Sorting based on TextBox value

  9. 9

    Update one form based on selections from another form

  10. 10

    VBA Userform Populate textbox with cell value based on 2 combobox selections

  11. 11

    Update based on Row number

  12. 12

    Update based on Row number

  13. 13

    how to use the listbox value to update textbox

  14. 14

    Delete a file based on the value of the SelectedItem in a ListBox

  15. 15

    Insert update based on value

  16. 16

    Vaadin 7 - update textarea at value changed

  17. 17

    Protractor to get value of textarea with line number

  18. 18

    Excel VBA: Searching for value in listbox based on value set in textbox

  19. 19

    Excel VBA: Searching for value in listbox based on value set in textbox

  20. 20

    How can I update a wxPython ListBox based on some search string?

  21. 21

    SQL - Need to update a value in a table based the number of relations its row has to another table

  22. 22

    Update row based on conditional value

  23. 23

    Write value based on number of rows

  24. 24

    Sort array based on number in value

  25. 25

    Count value based on name and number

  26. 26

    Update value based on the previous updated value mysql

  27. 27

    Jq update JSON key:value based on value

  28. 28

    Update checkbox and textarea value in mysql db using Jquery

  29. 29

    Update a column based on row_number()

HotTag

Archive