Javascript won’t output values from input box

Here’s a working version of your code.

  1. There’s no method getElementByName (but getElementsByName) – you should use document.getElementById() (Read about it here)
  2. You should use the value of the input element.
<!doctype html>
<html>
<head>

<style>
.formdiv{
    align: center;
}
</style>
</head>
<body>

<script language="javascript" type="text/javascript">
    function output(){
        var name = document.getElementById('firstName').value;
        var Age= document.getElementById('age').value;
        document.getElementById('output').value = name+Age;
    }
</script>

<h1><strong><em><center>Payment Details</center></em></strong> </h1>
<div class="formdiv">
<fieldset><center>
<legend> Enter the following Info:</legend>
<br />
<label> Name </label>
<input type="text" id="firstName" placeholder="John" required="required"></input> 
<br/>
<br/>
<label>Age </label>
<input type="number" id="age" maxlength="2" required="required"></input>
</fieldset>
</center>
</div>
<div>
<center>
        <button onClick="output()">Submit</button><br/>
<label for="output">Output</label>
<br/>
<input type="textbox" id="output"></input>
</center>
</div>
</body>
</html>

Leave a Comment