Skip to content

Try our new Crash Courses!

Become a member using the button below to hide this banner and gain access to our new Crash Courses.

Get value from input – JavaScript

Length: 10 minutes

Summary

const myInput = document.querySelector('#my-input');
console.log(myInput.value);

Details

To get the text inside of an input element, first create a variable and select the input.

const myInput = document.querySelector('#my-input');

To get the value, use the variable name followed by a period and then the word value. Here’s an example of grabbing the input value and printing it out to the console.

const myInput = document.querySelector('#my-input');
console.log(myInput.value);

Demo

Exercises

Recreate the JS code from the Demo section in your JS file.

References

input element on MDN

Back to: JavaScript Reference > JS DOM