Dmitry Dmitry - 1 month ago 25
Javascript Question

How to set a value of the textarea without id by javascript?

Typically we run javascript code to set any value:

document.getElementById('id_name').value = "...";


But I have a page like this:

<div id="id_name">
<div class="class_name">
<textarea></textarea>
</div>
</div>


How to set a value of the textarea by javascript?

Thanks a lot for help!

Answer

You could do this, if your HTML is really that simple:

var textarea = document.getElementById('id_name').getElementsByTagName('textarea')[0];
textarea.value = "hello world";

There's also a "getElementsByClassName()" in newer browsers that could be used to find the "class_name" <div> element, from which you'd do the "getElementsByTagName()" call.