'How can I convert the value written in HTML input to the string variable?

var imie = document.getElementById("imim");

function wypisz_imie() {
  document.getElementById("powitanie").innerHTML = "Czesc!" + imie;
}
<!DOCTYPE>
<html>

<head>
  <title>My city v1.0</title>
</head>

<body>
  <script type="text/javascript" src="skrypt.js"></script>
  <h2>Wpisz jak masz na imie</h2>
  <input type="text" id="imim" value="" />
  <input type="submit" id="potwim" value="potwierdz" onclick="wypisz_imie()" />
  <h1 id="powitanie"></h1>
</body>

</html>

Sorry for my English cause i'm from Poland and i'm new user. I'll be thankfull for answers!



Solution 1:[1]

You can use document.getElementById("imim").value to get the text in the text field. It's up to you what to do from there!

Solution 2:[2]

imie will return the input element so you should use value property like this:

var imie = document.getElementById("imim");
function wypisz_imie()
{
    document.getElementById("powitanie").innerHTML = "Czesc!" + imie.value;
}

imie.value will return the value of input. check w3s.

but every time the value will return "", to solve this problem you should access input every time call function to return input with new value like this:

function wypisz_imie()
{
    var imie = document.getElementById("imim");
    document.getElementById("powitanie").innerHTML = "Czesc!" + imie.value;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FrederickAmpsUp
Solution 2 Mohamed Mehdawy