evfwcqcg
evfwcqcg

Reputation: 16345

Add text to textarea

I'd like to add text "username" to textarea when some event is occurred, and this code works

$("textarea").append("username")

Exactly until I add some text to that textarea manually. How to avoid this behavior? This is what I have on html source.

<a href="javascript:void(0)" onclick="javascript:add_to_textarea('User')">User</a>

Upvotes: 0

Views: 781

Answers (3)

Bergi
Bergi

Reputation: 664513

Don't change the HTML/DOM of the textarea, but its value property (.val() in jQuery). This is a bit hackish, because a html <textarea> is only allowed to contain character data, just like a <input>s value attribute - though you code the text between the tags.

So if you want to add something, use

$("textarea").val(function(prev){ return prev+" "+username; });

Upvotes: 2

pete
pete

Reputation: 25081

Use $("textarea").val($("textarea").val() + "username");

Upvotes: 6

Minsc
Minsc

Reputation: 21

.val() perhaps? If you're trying to stick the value into the textarea box. Not sure what append actually does.

Upvotes: 2

Related Questions