Ticket #5641 (closed bug: duplicate)
Calling append on a textarea from a parent window returns old data
|Reported by:||DWard||Owned by:|
|Keywords:||textarea, append, old, value, val||Cc:|
I have a textarea on a page. Below are links, which each open a popup with multiple checkboxes. If one or more of these checkboxes are checked and their containing form submitted, jQuery loops through each option and appends (using the elem.append(string) function) the iterated checkbox value to the textarea on the parent page before closing the popup.
The append functions correctly if appending only, however...
If, for example 5 checkbox values were added to the the textarea, then the user decides they didn't want any of the values and it was all a mistake, then they would, naturally remove the values from the textarea. All is OK, until they decide to add, for example two values to the textarea now that they've cleared it. If they add these two new values, they will find that the earlier-selected values reappear in the textarea with the two new values on the end.
Instead of using the elem.append(string) function, use the elem.val(string) function to set the textarea (or any other input if also an issue) value with the concatenated current elem.val() and the value to append. Example below:
$(mySelector).val($(mySelector).val() + valueToAppend);
I'm not totally sure that this would be classed as a bug, but it has been an issue, as it's almost as if the value of the textarea is being cached instead of simply gathering the value of the target element upon appending its value.