[Solved] html form that passes information to a variable in javascript [closed]


Summary

I assume you are using a stand input control, and let us assume text… First you will have to put the form in your HTML, so in our JavaScript we can find the input using its the id. We also need a button, a regular button, not a submit button, for the user to click on when he decides to give in his input. We will attach a click event so that when our user clicks our JavaScript starts working So here is a start for our HTML:

<input type="text" id="input">
<button onclick="setClick()">
    Submit
</button>

Just before you say, “Yeah! I KNOW that!”, let us look at the JavaScript side… We need to first get the textbox’s value, So that when it is clicked the textbox’s value property is fetched and inserted into the variable input.. So here is the JavaScript:

function setClick()
{
    var input = document.getElementById('input').value; 
    //saves the current value of the textbox into the input variable
    alert(input);
}
/*
    So now each time the button is clicked the variable "input" is assigened
    the textbox's value
*/

Complete Code

<!DOCTYPE html>
<html>
<head>
<title>
Submit
</title>
<script type="text/javascript">
function setClick()
{
    var input = document.getElementById('input').value; 
    //saves the current value of the textbox into the input variable
    alert(input);
}
    /*
    So now each time the button is clicked the variable "input" is assigened
    the textbox's value
    */
</script>
<body>
<input type="text" id="input">
<button id="submit" onclick="setClick()">
    Submit
</button>
</body>
</html>

2

solved html form that passes information to a variable in javascript [closed]