Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery prepend to textarea text()

I have a text area. I can set the text of it with

$("#mytextarea").text("foo")

I can prepend to the text area like this:

$("#mytextarea").prepend("foo")

But I cannot prepend to the jquery text() object like this:

$("#mytextarea").text().prepend("foo")

The reason I want to do this is so that if my user gets me to prepend this text:

$("#mytextarea").prepend("<script>alert('lol i haxed uuu!')</script>")

...the script executes and I lose.

Help?

like image 781
shino Avatar asked Jun 06 '10 03:06

shino


People also ask

How do I add text to textarea?

To add text to a textarea, access the value property on the element and set it to its current value plus the text to be appended, e.g. textarea. value += 'Appended text' . The value property can be used to get and set the content of a textarea element.

Can we add image in textarea?

textarea is filled by user and user can only input texts in textarea . You can use drag and drop api to put image in textarea . Textarea is not a container tag hence not allow other tags inside it. It just provide multiple line input area to user.


1 Answers

You need to modify the val() property:

$('#mytextarea').val(function(index, old) { return '...' + old; });

By the way, the correct way to get the contents of the textarea is to call val(), not text():

like image 172
SLaks Avatar answered Oct 03 '22 03:10

SLaks