Jquery preend to textarea text ()

I have a text area. I can set its text with

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

I can add to the text area like this:

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

But I can not add jquery text () object as follows:

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

The reason I want to do this is because if my user forces me to add this text:

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

... the script is executed and I am losing.

reference

+5
source share
2 answers

You need to change the property val():

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

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

+9
source

URL API jQuery.val() -

http://api.jquery.com/val/

0

All Articles