0

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>

2
  • 1
    Your code gets the values of the fields once, when the page first loads. Move those first three lines of code inside the function. Commented Aug 26, 2017 at 13:45
  • Thanks, it worked Commented Aug 26, 2017 at 13:48

4 Answers 4

1

    /*
    var x = document.getElementById("fName").value;
    var y = document.getElementById("lName").value;
    var z = document.getElementById("emailId").value;
    */
        function myFunction(){
            var x = document.getElementById("fName").value;
            var y = document.getElementById("lName").value;
            var z = document.getElementById("emailId").value;
            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="numeric" 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>

Move variable

Global -> Local

Sign up to request clarification or add additional context in comments.

Comments

0

You are getting the values outside the function, in other words, you try to get the values of the input fields before you enter anything there. Try this instead of your javascript

<script>

        function myFunction(){
            var x = document.getElementById("fName").value;
            var y = document.getElementById("lName").value;
            var z = document.getElementById("emailId").value;
            document.getElementById("demo").innerHTML = x;
            console.log(x);
        }
</script>

Comments

0

You are extracting the values when the documents get ready. But you have to get the value when users submit the form.So you need to get the values inside the function try using this:

    function myFunction(){
         var x = document.getElementById("fName").value;
       var y = document.getElementById("lName").value;
        var z = document.getElementById("emailId").value;
        document.getElementById("demo").innerHTML = x;
        console.log(x);
    }

Comments

0

You only querying your values once as you load the page. What you can do is to query the fields and value inside your function. Or better: only check the values in your function and leave the getElementById still outside, like this:

var x = document.getElementById("fName");
var y = document.getElementById("lName");
var z = document.getElementById("emailId");

function myFunction() {
  document.getElementById("demo").innerHTML = x.value;
  console.log(x.value);
}
<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>

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.