2

I have a table with input fields inside the td. How can i get the values from the input fields (by button click) and store them into an array (one row) using JQuery ? So there are different rows with different contexts and i would like to process row by row, so i have to identify those rows by class and so on.

Thanks!

Update: I would like to have values from test1 in an array, test2 in an array and so on.

<tr class="test1">
 <td>
      <input type="text" name="test">
      <input type="text" name="test">
      <input type="text" name="test">
 </td>
</tr>

<tr class="test2">
 <td>
      <input type="text" name="test">
      <input type="text" name="test">
      <input type="text" name="test">
 </td>
</tr>
2
  • Can you place code ? or any effort u did ? Commented Aug 5, 2014 at 9:53
  • Can you paste your code to understand better? and easily we can help you in this regard. Commented Aug 5, 2014 at 9:54

2 Answers 2

5
$('#button').click(function () {
  var array = [];

  $('#table tr').each(function () {
    var values = [];

    $(this)
      .find('input')
      .each(function () {
        values.push(this.val());
      });

    array.push(values);
  });
});

then you can take items by array.pop();

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

Comments

0

you can do this way:

HTML:

<table>
    <tr class="test1">
        <td>
            <input type="text" name="test" />
            <input type="text" name="test" />
            <input type="text" name="test" />
            <td>
                <input type="button" class="GetValues" value="GetValues" />
            </td>
        </td>
    </tr>
    <tr class="test2">
        <td>
            <input type="text" name="test" />
            <input type="text" name="test" />
            <input type="text" name="test" />
        </td>
        <td>
            <input type="button" class="GetValues" value="GetValues" />
        </td>
    </tr>
</table>

JQUERY:

$(".GetValues").click(function () {
    var test1 = $(this).closest(".test1").find("input:text").map(function () {

        return $(this).val();

    });

    console.log(test1)

})

FIDDLE:

http://jsfiddle.net/XFy46/

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.