Read input from HTML

Given an input field
id="geld"
I want to save the input the user types into a field.

The HTML:
<input id="geld">

The JS:

let a = document.getElementById("geld").value;
    console.log(a);

Problem:
The input does not get console logged and also not saved.

Help appreciated.

Best,

S.

@Sven72, can you specify what is being logged?

@snigo Sorry, console.log(a) delivers nothing and I don’t know why.

It does log, but just an empty string since there’s no value in the input when the log is called.

If you write your HTML like this:

<input id="geld" value="hello">

Then it will log “hello”.

If you want to log every change, then you must add a listener, like:

let a = document.getElementById("geld");

a.addEventListener('input', () => console.log(a.value))
2 Likes