Mohammad Areeb Siddiqui
Mohammad Areeb Siddiqui

Reputation: 10179

Running script inside a textarea to print a value

Well the question is quite simple, How can I run the <script> tags inside a textarea so that I can use document.write to print out a value?

Like:

<textarea>
    <script>
        document.write("A");
    </script>
</textarea>

Doing this would just print out <script>document.write("A");</script> into the textarea.

See here: JSFIDDLE

But I want to print A inside the textarea how can I do that?

Upvotes: 1

Views: 5566

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222511

Here it is.

<textarea id="textMy"></textarea>
document.getElementById("textMy").innerHTML = 'A'

You miss the point of how JS works. You can not place it inside of some element hoping that it will print it inside of this element. You need to get that element and then modify it's properties somehow.

Upvotes: 2

Quentin
Quentin

Reputation: 943480

You can't place a script (or any other) element in a textarea, it's content model is defined as text.

The only way to generate the content of a textarea using document.write is to generate the entire textarea (including the start and end tags) using that method.

Upvotes: 1

Related Questions