Dmitry Dmitry - 9 months ago 92
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">

How to set a value of the textarea by javascript?

Thanks a lot for help!

Answer Source

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.