-3

I am trying to obtain the value of a specific text area on a form, in a separate javascript file.

I have tried code specified in this stackoverflow link: Get textarea by name attribute

however I still obtained null, as the output.

my code is here as follows:

 <td>
  <textarea name="key_input" rows="4" cols="50">
    testData
  </textarea>
 </td>

and in my js:

var key_Data = document.getElementById("key_input").value;

this is another way I have tried to obtain the key_input data, but I am still unsuccessful.

Any help would be much appreciated Thanks in advance.

Prash
  • 150
  • 9
  • 4
    getElementById will get an object by "id", not by "name", so no element will be found in your code. – Chris Dixon Feb 25 '19 at 11:51
  • 2
    give id to textarea as `id="key_input"` – Devsi Odedra Feb 25 '19 at 11:52
  • 1
    If you wondered why the code from the linked SO question did not work for you, that question was about using the jQuery library (which extends javascript). If you're not using that library, that style of code will not work on your page. – Cat Feb 25 '19 at 11:58
  • @Cat I thought the same, but then it's odd that the OP has tagged jQuery in this question. You'd assume in that case they were using it. – Rory McCrossan Feb 25 '19 at 12:02

6 Answers6

2

You are searching for an Id that doesn't exist, change name to id.

id="key_input"

And also, just creating the var doesn't show it.

var key_Data = document.getElementById("key_input").value;
console.log(key_Data);
<td>
  <textarea id="key_input" rows="4" cols="50">
    testData
  </textarea>
 </td>
1

Use document.querySelector('[name="key_input"]'):

console.log(document.querySelector('[name="key_input"]').value);
<td>
  <textarea name="key_input" rows="4" cols="50">
    testData
  </textarea>
 </td>
jo_va
  • 13,504
  • 3
  • 23
  • 47
0

You can add an specific id attribute to your textarea element and use Document.querySelector():

const textareaElem = document.querySelector('#key_input');

console.log(textareaElem.value)
<textarea id="key_input" name="key_input" rows="4" cols="50">
    testData
</textarea>
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

You can use jquery to do it simply

console.log($('#key_input').val())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td>
  <textarea name="key_input" rows="4" cols="50" id="key_input">
    testData
  </textarea>
 </td>
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
0

include the attribute "id" on your text area element

<td>
  <textarea name="key_input" id="key_input" rows="4" cols="50">
    testData
  </textarea>
 </td>

and your js

var key_Data = document.getElementById("key_input").value;

Seloka
  • 345
  • 1
  • 6
  • 20
0

function test(){
   alert(document.getElementsByName("key_input")[0].value)
  }
<textarea name="key_input" rows="4" cols="50">
    testData
  </textarea>
  <input type="button" onclick="test()" value="get data" />

you can use the above code for textarea value get by it's name

Shivam
  • 674
  • 1
  • 4
  • 25