Arianule
Arianule

Reputation: 9043

Retrieving values from multiple text inputs in jquery

I have multiple text boxes and textareas of which I want to add the values to an array.

With the way I am doing it at the moment I do manage to get the values of the text boxes but not the text areas.

How can I retrieve the values of multiple textarea?

<input type="text" name="fields[]"
<input type="text" name="fields[]"
<input type="text" name="fields[]"

<textarea name="areas[]"
<textarea name="areas[]"
<textarea name="areas[]"

This is the Jquery with which I do this.

var fields = [];
$('input[name^=fields]').each(function () {
    fields.push($(this).val());
});
var areas = [];
$('input[name^=areas]').each(function () {
    areas.push($(this).val());
});

I do manage to get the text box values but not the Multiline values(text area)

How can I do this?

Upvotes: 0

Views: 816

Answers (2)

Hamid Narikkoden
Hamid Narikkoden

Reputation: 861

But the best practice would be providing a class name for textarea and accessing values as follows :

<textarea class="areas"></textarea>
<textarea class="areas"></textarea>
<textarea class="areas"></textarea>


   var areas = new Array();
   $('.areas').each(function () {
         areas.push($(this).val());
   }); 

Check this : http://jsfiddle.net/Q9tm6/17/

Upvotes: 1

dkasipovic
dkasipovic

Reputation: 6120

Well obviously textarea's are not <input>. You should try something like:

var areas = [];
$('textarea[name^=areas]').each(function () {
    areas.push($(this).val());
});

Upvotes: 2

Related Questions