Phillip Senn
Phillip Senn

Reputation: 47605

Replacing text inside a textarea box

I have the following:

<textarea name="comment"><p>Good job.</p></textarea>

Using JavaScript, I'd like to replace "Good job" with "Here's how I graded your assignment:".

I'm not comfortable with string replacement yet.

Upvotes: 0

Views: 93

Answers (2)

mkoryak
mkoryak

Reputation: 57948

You have jquery on your user profile. so here is an answer using it:

$('textarea[name="comment"]').text("<p>Here's how I graded your assignment:</p>");

i would recommend adding a class to the textarea and selecting it via the class. The code above assumes you dont have another textarea with the name 'comment' on the page.

this would be better:

<textarea name="comment" class="some-better-name-here"><p>Good job.</p></textarea>
$('textarea.some-better-name-here').text("<p>Here's how I graded your assignment:</p>");

here it is in action: http://jsfiddle.net/MCVbg/

Upvotes: 4

caleb
caleb

Reputation: 1637

A pure Javascript solution:

var myTextArea = document.getElementsByName("comment")[0];
myTextArea.value = myTextArea.value.replace("Good job", "Here's how I graded your assignment:");

It'd be better to give your textarea and id and select it using getElementById instead.

Upvotes: 1

Related Questions