'How to show input (name + value) in textarea?

I'm working on this project where I need to have the value of a textarea change when one of the input values in the same form changes.

HTML:

<form id="form" action="" method="">
<textarea readonly class="overview"></textarea>
<input type="text" class="add" name="product1" />
<input type="text" class="add" name="product2" />
<input type="text" class="add" name="product3" />
<input type="text" class="add" name="product4" />
</form>

The customer is allowed to change the input.class. The textarea-value should change whenever one of the inputs changes.

The textarea should show something like:
3 x product1
1 x product3
4 x product4

Anybody got an idea?

Thanks in advance,

Joel



Solution 1:[1]

<textarea readonly class="overview" id="mytxtarea"></textarea>
   $("#product1").blur(function(){
   if($("#product1").val()!='')
     {
      $("#mytxtarea").val('test value');
     }
});

Solution 2:[2]

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Test</title>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#form .add').on('keyup', function(){
                console.log('test');
                var string1 = $('#form .product1').val() + ' x product1\n';
                var string2 = $('#form .product2').val() + ' x product2\n';
                var string3 = $('#form .product3').val() + ' x product3\n';
                var string4 = $('#form .product4').val() + ' x product4';

                $('#form textarea').val(string1 + string2 + string3 + string4);
            });
        });
    </script>
</head>
<body>
    <form id="form" action="" method="">
        <textarea readonly class="overview"></textarea>
        <input type="text" class="add product1" name="product1" />
        <input type="text" class="add product2" name="product2" />
        <input type="text" class="add product3" name="product3" />
        <input type="text" class="add product4" name="product4" />
    </form>
</body>
</html>

Solution 3:[3]

This will do what you want...

$("input.add").keyup(function() {
    var msg = "";
    $("input.add").each( function() {
        if( $(this).val() ) {
            msg += $(this).val() + "x " + $(this).attr("name") + "\n";
        }
    });
    $("textarea.overview").text(msg)
});

I used this with your HTML here: http://jsfiddle.net/XYXpp/

Solution 4:[4]

$('input').keyup(function () {
    var inputedValue = $(this).val();
    var name = $(this).attr('name');
    $(".overview").text(inputedValue+name);
});

Solution 5:[5]

var fd = new FormData()
fd.append('csrfmiddlewaretoken', document.getElementsByName('csrfmiddlewaretoken')[0].value)
fd.append('field_name', $("input[name='field_name']").serialize())

will return

['field_name=9&field_name=15&field_name=10']

you will then have to parse the info in your view

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepu
Solution 2
Solution 3 Smern
Solution 4
Solution 5 Drayen Dörff