-2

I currently have a webpage, and I want the user to be able to input their name and age. Once they've done that, they hit submit, and the first input is set to var name, and the second input box is set to var age. Is this possible?

Here's what I've tried

<form>
    <input type="text" Name:"firstname"><br>
    <input type="text" Age:"age"><br>
    <input type="submit" value="Done">
</form>

Im not sure how to go about setting the input to values however.

1
  • So read the input value and set it to a variable. Commented Oct 16, 2018 at 19:37

1 Answer 1

0

Firstly - your HTML markup is incorrect. I would sincerely suggest you read up on this.

https://www.w3schools.com/html/default.asp and https://www.w3schools.com/js/default.asp or any other site of your choice.

Secondly - once you correct your markup - you can do as follows:

<input type="text" id="firstname">
<input type="text" id="age">
<input type="button" id="submit" value="Set The Variables">

<script>
//Get the dom element you want to listen to and attach a click event
document.getElementById("submit").addEventListener("click", setVars); 


function setVars(){
//Set the variables
var firstname = document.getElementById("firstname").value;
alert ("Vars Set :" + firstname);
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.