-4

I am trying to get the value of a html that does not have an id.

Code -

<section class="post">
  <textarea rows="5" placeholder="(New Post)"></textarea>
</section>

JS i've tried::

var entryText = document.textarea.value;
var entryText = document.getElementsByTagName.value;
  • 1
    `HTMLElement.getElementsByTagName` will return an HTML collection. You only want to get the value for a single element. – Tanner Dolby Mar 10 '21 at 20:55
  • Does this answer your question? [Javascript: How to get only one element by class name?](https://stackoverflow.com/questions/21436550/javascript-how-to-get-only-one-element-by-class-name) – Ivar Mar 10 '21 at 20:56

1 Answers1

1

You can use the more modern document.querySelector() for this task to select the first textarea that resides in a section block that has the class post:

var entryText = document.querySelector("section.post textarea").value;
console.log(entryText);
<section class="post">
  <textarea rows="5" placeholder="(New Post)">This is some default text, to show that the call to console.log() works.</textarea>
</section>

You may wish to dive further into the documentation for CSS selectors, which is what document.querySelector()'s argument takes.

esqew
  • 42,425
  • 27
  • 92
  • 132