0

In a HTML website I have a textarea created like this:

<textarea id = "myTextArea" rows = "30" cols = "80"></textarea>

I would like after something is written in the text area, for that text to be sent to a variable in javascript.

I have tried doing this, but it did not work:

var x = document.getElementById("myTextArea").value;

The console.log(x); gives back nothing, not null, just empty space. However, if I log out console.log(document.getElementById("myTextArea").value) then I get the text that I have written in my textarea.

Why does var x = document.getElementById("myTextArea").value; not work?

My Javascript:

<script>
var x = document.getElementById("myTextArea").value;
const regex = /([a-z]+)/;
const match = regex.exec(x);

var intervalID = window.setInterval(myCallback, 500); <!-- Calls every 5s -->

function myCallback() {
    if(match){
       const name = match[1];
       console.log(name);
    }
    else{
       console.log('no match');
       console.log(match);
    }
}
4
  • How do I ask a good question? -> minimal reproducible example -> "Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question." Commented Nov 11, 2020 at 10:44
  • Your textarea has no text so value is empty? The syntax looks ok to me. Commented Nov 11, 2020 at 10:47
  • In the beginning, yes, but even if I write something it still doesn't log anything. Commented Nov 11, 2020 at 10:50
  • Please add a minimal reproducible example Commented Nov 11, 2020 at 10:50

3 Answers 3

2

To achieve this, you can register an event listener on your textarea:

var textArea = document.getElementById('myTextArea');

textArea.addEventListener('input', function(event){
    console.log(event.target.value);
});

The listener listens for any input events on your textara and logs the value of your textarea as the value changes.

Here's a live demo for your quick reference.

Sign up to request clarification or add additional context in comments.

Comments

0

You will need to use onkeyup and onchange for this. The onchange will prevent context-menu pasting, and the onkeyup will fire for every keystroke.

See my answer on How to impose maxlength on textArea for a code sample.

Comments

0

In my example, the variable is the text variable. This variable is filled with the text of the text by clicking on the button.

Was it necessary?

var x = document.getElementById("myTextArea");
var button = document.querySelector("button");
var text = "";

button.onclick = function() {
  text = x.value;
  console.log(text);
}
#myTextArea {
  width: 100%;
  height: 100px;
}
<textarea id="myTextArea" rows="30" cols="80"></textarea>
<button>Check variable with text</button>

Second example using oninput event.

var x = document.getElementById("myTextArea");
var text = "";

x.oninput = function() {
  text = this.value;
  console.log(text);
}
#myTextArea {
  width: 100%;
  height: 100px;
}
<textarea id="myTextArea" rows="30" cols="80"></textarea>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.