0

I am fetching the values from the form page and then validating it. I am using jquery to handle the input objects and fetch the values which are entered by the user.

My HTML code is:

<form id="userDetails">

  <table class="containFormTable" cellspacing="8">

    <tr>
      <td class="detail">Name:</td>
      <td>
        <input name="name" type="text" id="name" />
      </td>
    </tr>

    <tr>
      <td class="detail">Email-Id:</td>
      <td>
        <input name="emailId" type="text" id="emailId">
      </td>
    </tr>

    <tr>
      <td class="detail">Phone-No:</td>
      <td>
        <input name="phoneNo" type="text" id="phoneNo">
      </td>
    </tr>

    <tr>
      <td colspan="2" style="padding-top: 50px">
        <input type="button" value="Submit" id="submit">
      </td>
    </tr>



  </table>


</form>

My javascript file code which is fetching the data is:

$(document).ready(function() {
  $('#submit').click(function() {

    var name = $('#name').find('input[name="name"]').val();

    var emailId = $('#emailId').find('input[name="emailId"]').val();

    var phoneNo = $('#phoneNo').find('input[name="phoneNo"]').val();

  });
});

The result I am getting in the variable name or any other is "undefined". Where am I going wrong in it?

2 Answers 2

1

All you need to do is get the value from the elements, like this:

var name = $('#name').val();
var emailId = $('#emailId').val();
var phoneNo = $('#phoneNo').val();

Your code was getting a reference to the form fields, then looking for child elements (which input elements don't have, hence undefined) that were the same as the element that you already found and then trying to get the value of that.

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

Comments

0

Just remove .find('input[name="name"]') from each selector

$(document).ready(function(){




                        $('#submit')
                            .click(
                                    function() {

                                        var name = $('#name').val();

                                        var emailId = $('#emailId').val();

                                        var phoneNo = $('#phoneNo').val();

console.log(name);
                                      console.log(emailId);
                                      console.log(phoneNo);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="userDetails">

                <table class="containFormTable" cellspacing="8">

                    <tr>
                        <td class="detail">Name:</td>
                        <td><input name="name" type="text" id="name"/></td>
                    </tr>

                    <tr>
                        <td class="detail">Email-Id:</td>
                        <td><input name="emailId" type="text" id="emailId"></td>
                    </tr>

                    <tr>
                        <td class="detail">Phone-No:</td>
                        <td><input name="phoneNo" type="text" id="phoneNo"></td>
                    </tr>

                    <tr >
                    <td colspan="2" style="padding-top: 50px"><input type="button" value="Submit" id="submit"></td>
                    </tr>



                </table>


            </form>

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.