1

I have two tables on the page. Inside of my second table I am dynamically adding html from JS. I am checking to see if the <tr> inside of the tbody are empty using:

var rowCount = $('#dynatable >tbody >tr').length;

If the row count is 0 I continue on with my javascript, if 1 or more <tr> exists I need to go through each <tr> and grab the two input values inside the <td>; for every <tr> available. Then need to collect the data into an array for server-side processing.

<table id="dynatable">
<thead>
    <tr>
        <th>Time</th>
        <th>Description</th>
    </tr>
</thead>
<tbody id="p_scents">
    <tr><td class="ts_td"><input type="text" name="ts_value[]" class="timestamp_input"/></td><td><TEXTAREA NAME="ts_description[]" class="ts_desc" rows="3" cols="30"  style="resize:none;" ></TEXTAREA></td></tr>
    <tr><td class="ts_td"><input type="text" name="ts_value[]" class="timestamp_input"/></td><td><TEXTAREA NAME="ts_description[]" class="ts_desc" rows="3" cols="30"  style="resize:none;" ></TEXTAREA></td></tr>
</tbody>

I have browsed similar questions but I cannot figure out a solution for my situation. I had a previous solution using simpleHTMLDOM + PHP but it's way too slow performance wise.

I was told to use JS or jquery and I am quite unfamiliar with both, any help is greatly appreciated. How do I grab the values of multiple inputs from multiple <tr>'s inside a specific table and store the values in an array?

2
  • 1
    can form encode it by simply wrapping table in <form> and using serialize() Commented Sep 11, 2014 at 19:43
  • I will look into serialize(), thank you. Commented Sep 11, 2014 at 19:45

1 Answer 1

1

If your classes will be the same, you could do something like this with jQuery.

It put the values into arrays.

var values = $("input[class='timestamp_input']").map(function(){return $(this).val();}).get();

var textAreaValues = $("textarea[class='ts_desc']").map(function(){return $(this).val();}).get();

console.log(values);

console.log(textAreaValues);

JSFiddle demo here.

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

1 Comment

Yes the classes will be the same name so this will work. Thank you for the solution.

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.