0

I'm trying to get the values from a form I've created in HTML: I'm using Javascript to try and get the values from the form, I've tried multiple ways of getting the values, at the moment it's this

$(document).ready(function() {
  $('#submit').click(function() {
    var x = document.getElementById("input");
    var text = "";
    var i;
    for (i = 0; i < x.length; i++) {
      console.log(x.elements[i].value);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="input">
  First Name: <br/>
  <input type="text" name="firstName"/><br/>

  Last Name: <br/>
  <select id="lastName" style="text-align: center;">
  </select><br/>

  <p>Date of Birth:</p><br />

  Day: <br/>
  <select id="dayDropDown" style="text-align: center;">
  </select><br/>
  Month: <br/>
  <select id="monthDropDown" style="text-align: center;">
  </select><br/>
  Year: <br/>
  <select id="yearDropDown" style="text-align: center;">
  </select><br/>

  Country: <br/>
  <input type="text" name="country"/><br/>
</form>
<br/>
<button id="submit" type="button">Submit</button>

This is meant to output the values from the form to my VS the output window, but I get nothing. any ideas?

7
  • 1
    use jquery : var queryString = $('#input').serialize(); Commented Oct 31, 2016 at 11:26
  • Use this : var x = document.getElementsByTagName("input") Commented Oct 31, 2016 at 11:26
  • var formData = $('#input').serialize(); Commented Oct 31, 2016 at 11:27
  • Is document.getElementById(element).elements a thing? Are you sure shouldn't be using .childNodes? Commented Oct 31, 2016 at 11:27
  • 1
    @Rory I realized that later... I edited my comment Commented Oct 31, 2016 at 11:29

6 Answers 6

2

As you're already using jQuery to attach your event handler, you may as well use it to select all the elements in the form using the :input selector. From there you can simply loop through them all using each() and output their values to the console, like this:

$(document).ready(function() {
  $('#submit').click(function() {
    $('form :input').each(function() {
      console.log($(this).val());
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="input">
    First Name: <br/>
    <input type="text" name="firstName"/><br/>

    Last Name: <br/>
    <select id="lastName" style="text-align: center;">
    </select><br/>

    <p>Date of Birth:</p><br />

    Day: <br/>
    <select id="dayDropDown" style="text-align: center;">
    </select><br/>
    Month: <br/>
    <select id="monthDropDown" style="text-align: center;">
    </select><br/>
    Year: <br/>
    <select id="yearDropDown" style="text-align: center;">
    </select><br/>

    Country: <br/>
    <input type="text" name="country"/><br/>
</form>
<br/>
<button id="submit" type="button">Submit</button>

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

9 Comments

Not sure about the downvoters, but I dont think this answers the original question. His code works (even though it's not a good javascript/jQuery code, and can be written much better with jQuery...)
Good approach, but I assume the OP also wants the values of the select fields too, if they would be filled. (+1 although)
@secelite this will get that. There are just no options in his current HTML to retrieve
@RobertAKARobin very possibly. However it would depend on what OPs intended use is. If he wants to work with the values directly in this loop then there's no point looping to create an array and looping again to process it. If he wants to store the array for use in multiple places the serializeArray() would be more appropriate
@RoryMcCrossan I wasn't aware of this. Thanks for also expending my knowledge.
|
1

You can use jQuery and get the data with one statement

var queryString = $('#input').serialize();

See this live example for a demo.

1 Comment

.serializeArray would probably be closer to what OP wants.
0

getElementsByTagName will solve your problem.

This is your code as modificated:

 $(document).ready(function () {
        $('#submit').click(function () {
            var x = document.getElementsByTagName("input");
            var text = "";
            var i;
            for (i = 0; i < x.length; i++) {
                console.log(x[i].value);
            }
        });
    });

2 Comments

Seems odd to use vanilla JS when JQuery's already being used, plus this won't work for the select elements.
this just for inputs. I you want to run it for selects too, you should add this var y = document.getElementsByTagName("select") then concat these arrays.
0

Apply with same Id name with all input . your javascript was working ...

$(document).ready(function () {
        $('#submit').click(function () {
            var x = document.getElementById("input");
            var text = "";
            var i;
            for (i = 0; i < x.length; i++) {
                console.log(x.elements[i].value);
            }
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="input">

        First Name: <br/>
        <input type="text" name="firstName" id="input"/><br/>

        Last Name: <br/>
        <select id="lastName" style="text-align: center;" id="input">
        </select><br/>

        <p>Date of Birth:</p><br />

        Day: <br/>
        <select id="dayDropDown" style="text-align: center;" id="input">
        </select><br/>
        Month: <br/>
        <select id="monthDropDown" style="text-align: center;" id="input" >
       
  </select><br/>
        Year: <br/>
        <select id="yearDropDown" style="text-align: center;" id="input">
        </select><br/>

        Country: <br/>
        <input type="text" name="country" id="input"/><br/>
    </form>
    <br/>
    <button id="submit" type="button">Submit</button>

Comments

0

I'd suggest that you add the jquery-serialize-object reference,so it may do the things become a bit easier:

 $('#input').submit(function(){
   var objForm = $(this).serializeObject();
    console.log(objForm);
 });

Here you can find other examples and the jquery-serialize-object library for downloading

Comments

-1
$(document).ready(function () {
    $('#submit').click(function () {
        $('form input, form select').each(function () {
            console.log($(this).val());
        });
    });
});

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.