Change text displayed in HTML textarea using JavaScript

How do I change the text displayed in an HTML textarea using JavaScript?  Setting either value or defaultValue appears to change the attribute in the DOM but not the display. Please see code snippet.
testTextareaValue.html
anAppBuilderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I believe you have to use 'innerHTML' to change the text in between the opening and closing tags in 'textarea'.
1
anAppBuilderAuthor Commented:
Thank you, Dave.  Unfortunately that does not work.  Please see my new test snippet.  It does not work in IE, Edge, or Chrome.  I did not test any other browsers.

testTextareaValue.html
0
Dave BaldwinFixer of ProblemsCommented:
You're trying way too hard.  This works.
<!doctype html>
<html id="myHTML">
<head>
	
	<script>
	function testTextareaValue() {
		var elt = document.getElementById("myText");
		elt.innerHTML = "This is the changed text";
		return;
	}
	</script>
	<meta charset='UTF-8'>
	
</head>

<body id = 'myBody'>
		
	<form id = 'myForm' >
	<textarea id='myText' rows='20' cols='120'>Initial text</textarea>
	</form> 
	<br><br>
	<button type='button' value = 'test' onclick='testTextareaValue()'>Test Text Area</button>
	
</body>
</html>

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anAppBuilderAuthor Commented:
Thank you, Dave,

I think it is always valuable to determine why the solution that didn't work failed.  In this case, the snippet I posted appeared to differ from the solution only by doing several debug writes. And with some testing I determined that the debug writes were causing the failure. Any writes to the body element of the form "body.innerHTML += ..." prevent the update to the text in the TextArea.  And interestingly doing an "elt.focus();" back to the TextArea after the body write(s) does not prevent the failure.  My bad--should have used console.log for the debug writes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.