Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use javascript to set text area value in a form in IE

I can use this to set a text area (selectedtext) value in a form (submitquestion) if Firefox, but it fails in IE.

document.submitquestion.selectedtext.value = txt;

like image 758
user570494 Avatar asked Apr 08 '11 00:04

user570494


People also ask

Does textarea have value attribute?

<textarea> does not support the value attribute.

What is HTMLTextAreaElement?

The HTMLTextAreaElement interface provides special properties and methods for manipulating the layout and presentation of <textarea> elements.

How do I display textarea content in HTML?

Use the <textarea> tag to show a text area. The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows. Specifies that on page load the text area should automatically get focus.


3 Answers

This should work:

<textarea id="bla">from</textarea>
<script type="text/javascript">
document.getElementById("bla").value = "test";
</script>
like image 53
ysrb Avatar answered Nov 15 '22 00:11

ysrb


Try this:

document.forms['submitquestion'].elements['selectedtext'].value = txt;

Assuming you have:

<form name='submitquestion'>
    <textarea name='selectedtext'></textarea>
</form>
like image 28
moe Avatar answered Nov 15 '22 00:11

moe


I recommend using JQuery, it works with all browsers.

$('#selectedtext').val('whatever');
like image 45
Richard Schneider Avatar answered Nov 14 '22 23:11

Richard Schneider