1

I have two input boxes and two select boxes and would like to display a summary of data selected and entered in real time prior to submitting the form. jQuery seems the way to go, but how!

<form id="form1" name="form1" method="post" action="">
<select name="Select1" id="Select1">
  <option value="Item 1">Item 1</option>
  <option value="Item 2">Item 2</option>
  <option value="Item 3">Item 3</option>
</select>
<br />
<select name="OtherItem" id="OtherItem">
  <option value="OtherItem 1">OtherItem 1</option>
  <option value="OtherItem 2">OtherItem 2 </option>
  <option value="OtherItem  3">OtherItem 3</option>
</select>
<input type="text" name="textfield1" id="textfield1" />
<input type="text" name="textfield2" id="textfield2" />
</form><div id="FormSummary"></div>

Many thanks

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Richbits
  • 45
  • 1
  • 4

5 Answers5

2

I would add an HTML markup for your summary so:

<div id="FormSummary">
     <p>Select 1 is: <span id="summarySelect1"></span></p>
     <p>Select 2 is: <span id="summarySelect2"></span></p>
     <p>Textfield 1 is: <span id="summaryTextField1"></span></p>
     <p>Textfield 2 is: <span id="summaryTextField2"></span></p>
</div>

then ur jquery:

$('#Select1').change(function(){
     $('#summarySelect1').text($(this).val());
});

$('#Select2').change(function(){
     $('#summarySelect2').text($(this).val());
});

$('#textfield1').keypress(function(){
     $('#summaryTextField1').text($(this).val());
});

$('#textfield2').keypress(function(){
     $('#summaryTextField2').text($(this).val());
});
Evan
  • 5,975
  • 8
  • 34
  • 63
1

The best I could come up with is the following, using on():

$('input, select, textarea')
    .on('change',
        function(e){
            var inputs = $('input:text').map(
                function(){
                    return $(this).val();
                        }).get().join(', ');
            var selects = $('select option:selected').map(
                function(){
                    return $(this).text();
                });
            var textareas = $('textarea').map(
                function(t){
                    return $(this).val();
                }).get().join();
            $('#FormSummary')
                .html('<p>Inputs: ' + inputs + '.</p><p>Selects: ' + selects + '.</p><p>Textareas: ' + textareas + '</p>');
        });

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0
$('select').each(function() {
    $('#FormSummary').append('<div>' + $(this).val() + '</div>');
});

$('input').each(function() {
    $('#FormSummary').append('<div>' + $(this).val() + '</div>');
});
Viruzzo
  • 3,025
  • 13
  • 13
0

.change() is the method you're looking for. It listens to changes to

<input> elements, <textarea> boxes and <select> elements

So you can do something like this:

$('select, input').change(function(){
  // triggered each time a drop-down or input changes
  var preview = ''
  $('select, input').each(function(){
    // gets all of the user inputs, and add the input value to a string
    user_value = $(this).val()
    preview = preview + user_value
  })
  // displays the combined input to the user
  $('#preview').text(preview)
})
Hoff
  • 38,776
  • 17
  • 74
  • 99
0

Bind the summary function to any change events you want to. In the summary function read the selected values (.val()) and print it into your summary element (or better: its children, as Evan suggested). Example:

jQuery(function($) {
    var form = $('#form1').bind('input', function updateSummary() {
        var summary = "";
        form.find('select, input').val(function(v) {
            summary += " "+v;
            return v;
        });
    });
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375