0

I want to be able to have a user write some form of data to a html input field, and then have that data copied and saved to a javascript variable that I can evaluate later.

I'm fairly new to javascript, and right now i have an input and a button set up on a HTML page.

<input id='input' type='text' placeholder='Enter your action...'>
<button id='button'>Enter</button>

button.addEventListener('click', function() {
let userData = inputAfterButtonClick
}

I expect it to save the data the user has typed and save it to a variable.

  • 2
    I would say this a very basic concept. You should learn fundamentals of JavaScript first before anything, if you are planning to work with JS. You could refer to W3Schools or Mozilla Dev Network to learn basics. – Azaz ul Haq Dec 28 '18 at 06:34
  • Possible duplicate of [How do I get the value of text input field using JavaScript?](https://stackoverflow.com/questions/11563638/how-do-i-get-the-value-of-text-input-field-using-javascript) – pellul Dec 28 '18 at 08:12

2 Answers2

0

There are various methods to get an input value. You can also wrap it up in a form element.

Make use of document.getElementById

document.getElementById('id').value to get the value of desired input element

For example:

    <input id='input' type='text' placeholder='Enter your action...'>

    document.getElementById("input").value;

This is a very basic concept so i would suggest you to learn basic concepts in JavaScript and HTML. WebSites like w3schools.com or tutorialspoint.com might be helpful.

subodhkalika
  • 1,964
  • 1
  • 9
  • 15
  • Thank you very much for your input. I was aware of getElementById, but was fundamentally unsure of its application, and thank you for showing me. – FascismSalad Dec 28 '18 at 08:48
0

With Javascript-

var input = document.getElementById('userInput').value;
alert(input);

With Jquery-

var input = $('#userInput').val();
alert(input);
ellipsis
  • 12,049
  • 2
  • 17
  • 33