So I have been learning the basics of the JavaScript language. So far so good. I'm practicing a lot of comparative operations to understand various calculations and outputs based on my variable definitions.
I can define 2 variables and set them. That's easy.
var variable1 = 1;
var variable2 = 2;
I can use an if/else statement to compare the 2 variable values.
if (variable1 > variable2) {
alert("The first variable is greater than the second.");
} else {
alert("The second variable is greater than the first one.");
}
I understand this simple logic and how this works.
My question is, if I want a webpage user to type in 2 numbers so I can calculate them (using the above conditional statement) how do I access or define the variables that are the result of user input? So far I can only define the variables myself in a js file. How would I access user html input in javaScript to perform the same calculation?
My first assumption would be that I use the getElementById property to access the value of a textarea element in html. But I am not sure how this would store the textarea value as a variable to then be calculated. I hope this makes sense.
Thank you to those who will help me with this. I appreciate that your time is important and that this is a VERY basic question for many of you.
var variable1 = document.getElementById('inputfield1').value;window.prompt()can be a useful crutch before learning about extracting values from HTML form input. Usage:variable1 = window.prompt("Enter a value for variable1:");