user2245052
user2245052

Reputation: 19

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

Upvotes: 0

Views: 1797

Answers (6)

Drayen D&#246;rff
Drayen D&#246;rff

Reputation: 103

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

Upvotes: 0

user1969752
user1969752

Reputation:

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

Upvotes: 0

Nirmal
Nirmal

Reputation: 2368

  $(".add").keyup(function () {
 var app=""
 $(".add").each(function () {
       if( $(this).val() ) {
     var value=$(this).val();
     var name=$(this).attr('name');
     app +=value+"*"+name+ "\n" ;     
       }
 });
  $(".overview").text(app);

});

Watch this.

Hope this will give you some solution.

Upvotes: 0

manuAcl
manuAcl

Reputation: 311

<!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>

Upvotes: 1

Smern
Smern

Reputation: 19086

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/

Upvotes: 0

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

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

Upvotes: 1

Related Questions