So, I was working on a simple Form validation. But after entering the value when I try to get the value from the input element in the console using JavaScript, it is returning blank.
Where am I going wrong:
Here is the code:
var x = document.getElementById("fName").value;
var y = document.getElementById("lName").value;
var z = document.getElementById("emailId").value;
function myFunction() {
document.getElementById("demo").innerHTML = x;
console.log(x);
}
<h1 class="jumbotron">Simple Form Validation</h1>
<div class="container-fluid">
<form>
<label>First Name: <input type="text" value="" id="fName" placeholder="ex: Shubham"></label><br />
<label>Second Name: <input id="lName" value="" type="text" placeholder="ex: Bhatt"></label><br />
<label>Email Id: <input id="emailId" value="" type="alphanumeric" placeholder="ex: [email protected]"></label> <br />
<input type="button" value="Submit" id="submitBtn" class="btn btn-primary" onclick="myFunction()">
<p id="demo"></p>
</form>
</div>