Answer the question
In order to leave comments, you need to log in
How to pass the value of an input field to a JavaScript variable, and then display this value on another page of the site?
Hello! I'm a beginner, don't judge strictly: It is
necessary to pass the values of the filled form fields (the form is on the first page):
<body>
<form name="n1">
<fieldset>
<legend>Пример формы</legend>
<p>
<label for="field-1">Поле 1</label>
<input type="text" id="field-1" name="value1">
</p>
<p>
<label for="field-2">Поле 2</label>
<input type="text" id="field-2" name="value2">
</p>
</fieldset>
<p><input type="button" value="Отправить" onClick="Complete();"></p>
</form>
<p id="val1"></p>
<p id="val2"></p>
<script src="p3.js"></script>
</body>
<body>
<p id="val1"></p>
<script src="p3.js"></script>
</body>
function Complete()
{
var v1 = document.n1.value1.value;
var v2 = document.n1.value2.value;
document.getElementById("val1").innerHTML = v2;
alert(v1);
}
Answer the question
In order to leave comments, you need to log in
Use localStorage to share data between pages. The simplest crutch
Add an input attribute
name="test"
Then wrap the input along with the button in
<form method="GET" action="путь к вашей странице">сюда кнопку и input</form>
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('test');
window.addEventListener("load", function() {
const el = document.querySelector("primer1");
el.innerText=myParam;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question