A Hagrasi
A Hagrasi

Reputation: 25

How to get Values of Group of textbox and put them in a ( key : Value ) array with jquery?

How to get values of group of textbox and put them in a (key : value) array using JQuery?

foreach ($students as $value) {
    ?>
    <tr>
        <td>
            <input name="result[]" class="result" id="<?= $value['stuId'];?>" type="text" required=""/>
        </td>
    </tr>
    <?php
        }
    ?>

I don't know how to get values of a group of input values.

Which technique is useful in that case and how can I make the keys of the array is the input id?

Upvotes: 1

Views: 1550

Answers (1)

sinisake
sinisake

Reputation: 11318

Demo: http://jsfiddle.net/uyko2ahx/

Depending on your needs, you can use simple array, or object, with key:value pair:

arr=[];
$( ".result" ).each(function( index ) {
 key=$( this ).prop('id');
 value=$(this).val();
    arr.push(key+':'+value);   
});

console.log(arr);

obj={};

$( ".result" ).each(function( index ) {
 key=$( this ).prop('id');
 value=$(this).val();

    obj[key]=value; 
});

console.log(obj);

Upvotes: 1

Related Questions