开发者

jquery prepend to textarea text()

开发者 https://www.devze.com 2023-01-02 05:09 出处:网络
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:

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("开发者_如何学Cfoo")

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?


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():


Please check the following URL for jQuery .val() API documentation for more details-

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

0

精彩评论

暂无评论...
验证码 换一张
取 消