Read input from HTML

Read input from HTML
0

#1

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.


#2

@Sven72, can you specify what is being logged?


#3

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


#4

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))