0

I've a form and on button submit, how can i echo out all the values within the input tags in alert box?

My code as below:

$(document).ready(function(e){
    $('#divID').on('submit',function(){
    alert($('#divID input').val());
    });
});
Origami
  • 115
  • 5
  • 14

3 Answers3

1
$(document).ready(function(e){
    $('#divID').on('submit',function(){
       $("#divID input").each(function(index){
           alert($(this).val());
       });
    });
});
areeb
  • 424
  • 3
  • 9
  • While this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight Apr 04 '16 at 12:08
0

Say you have an html form like that:

<form id="myForm">
<input type="text" name="field1" id="field1" />
<input type="text" name="field2" id="field2" />
<input type="text" name="field3" id="field3" />
<input type="text" name="field4" id="field4" />
<input type="button" name="submit" id="submit" value="Submit" />
</form>

The script that should handle things when "submit" is pressed could look like that:

$(document).ready(function(){
    $('#submit').on('click', function(e){
        e.preventDefault //do not submit right away
        var thisFormData = $('#myForm').serialize(); //serialize all form data
        alert(thisFormData); //alert serialized form data
        $('#myForm').submit(); //submit the form after fields/values have been alerted
    });
}); 

Hope it helps you! T.

Theo Orphanos
  • 1,417
  • 1
  • 19
  • 27
0

You can use jQuery .each() to alert all values in input fields one by one like below :

$(document).ready(function(){
    //#submit should be id of your submit button
    $('#submit').on('click', function(e){
        e.preventDefault //stop default form submit
        var thisFormData = $('#myForm').serialize(); //serialize all form data
        $('#submit input').each(function(){
            alert($(this).val(); //alert each input fields value
        });

        $('form#myForm').submit(); //submit the form after alert is completed.
    });
});

$(document).ready(function() {
  //#submit should be id of your submit button
  $('#submit').on('click', function(e) {
   
    //alert only input type=text values
    $('form#myForm input[type="text"]').each(function() {
        console.log($(this).val());//better way is to use console and not alert
        alert($(this).val()); //alert each input fields value
    });

    $('form#myForm').submit(); //submit the form after alert is completed.
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="myForm">
  <input type="text" name="field1" id="field1" />
  <input type="text" name="field2" id="field2" />
  <input type="text" name="field3" id="field3" />
  <input type="text" name="field4" id="field4" />
  <input type="button" name="submit" id="submit" value="Submit" />
</form>
shivgre
  • 1,163
  • 2
  • 13
  • 29