1

I have 3 text input fields like first-name,last-name and age and a ADD button. I also have a textarea where I want to display the added data as a value of textarea when click on ADD button.

I write some code but its only giving me the latest value of 3 input fields data inside textarea but I want to display all added data with comma separated inside textarea. example: Arora Shetty 25, Alok Kumar 30 etc. using jquery.

Here is the code i wrote

<input type="text" id="first_name" name="first_name"> 
<input type="text" id="last_name" name="last_name">
<input type="text" id="age" name="age">
<textarea id="fetch_here"></textarea>
<button class="btn btn-success" id="add_data">ADD</button>

jQuery code:

$(document).ready(function() {
  $('#add_data').click(function() {
    var first_name = $('#first_name').val();
    var last_name = $('#last_name').val();
    var age = $('#age').val();
    var user_data = $('#fetch_here').val(first_name + ' ' + last_name + ' ' + age);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first_name" name="first_name">
<input type="text" id="last_name" name="last_name">
<input type="text" id="age" name="age">
<textarea id="fetch_here"></textarea>
<button class="btn btn-success" id="add_data">ADD</button>

How can i add multiple data inside textarea?

3
  • add_data != add_flower Commented Jun 8, 2018 at 8:30
  • And it works fine in the snippet. What's the problem? Commented Jun 8, 2018 at 8:30
  • 1
    I need to add multiple data. right now its adding the most recent data if you update the last data is gone. I need to grab all data that added and no data will disappear Commented Jun 8, 2018 at 8:32

3 Answers 3

2

I suggest you to use an array to simplify your code, then, use:

  • .push() to add a piece of data,
  • .join() to join all the array elements with the string you want,

Then, you only need to get the current value of the textarea before replacing (adding) its content.

$('#add_data').click(function() {
  var data = [];
  data.push($('#first_name').val());
  data.push($('#last_name').val());
  data.push($('#age').val());
  var current_val = $('#fetch_here').val();
  $('#fetch_here').val(current_val + data.join(' ') + '\n');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first_name" name="first_name">
<input type="text" id="last_name" name="last_name">
<input type="text" id="age" name="age">
<textarea id="fetch_here"></textarea>
<button class="btn btn-success" id="add_data">ADD</button>

Hope it helps.

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

Comments

1

You need to get the previous text from the textarea before you assign the new value into it:

$(document).ready(function(){
  $('#add_data').click(function(){
     var first_name = $('#first_name').val();
     var last_name = $('#last_name').val();
     var age = $('#age').val();
     var user_data = $('#fetch_here').text(
       $('#fetch_here').text() +
       first_name+' '+last_name+' '+age + '\n' );
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first_name" name="first_name"> 
<input type="text" id="last_name" name="last_name">
<input type="text" id="age" name="age">
<textarea id="fetch_here"></textarea>
<button class="btn btn-success" id="add_data">ADD</button>

3 Comments

@user3443037 I gave you the first answer. This is not fair at all.
@PraveenKumar don't act childish. Is that the rule that who gave first answer need to be accepted
@AnkitAgarwal Agree with you on being childish, but isn't that fair. LoL. Bro, everyone spend their time here to get something out... Anyway... :)
1

Alright for multiple data you need to append.

$(document).ready(function() {
  $('#add_data').click(function() {
    var first_name = $('#first_name').val();
    var last_name = $('#last_name').val();
    var age = $('#age').val();
    var user_data = $('#fetch_here').val($('#fetch_here').val() + "\n" + first_name + ' ' + last_name + ' ' + age);
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="first_name" name="first_name">
<input type="text" id="last_name" name="last_name">
<input type="text" id="age" name="age">
<textarea id="fetch_here"></textarea>
<button class="btn btn-success" id="add_data">ADD</button>

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.