0

I have a table and need to build json array. I need value from 1 and 4 column of table.

HTML

<table class="table table-striped" id="development_mapping">
                                <thead>
                                    <tr>
                                        <th>Visual Feature</th>
                                        <th>Step</th>
                                        <th>Output</th>
                                        <th>Data Feature</th>
                                    </tr>
                                </thead>
                                <tbody>                      
                                <tr><td>color</td> 
                                        <td><select id="sss"><option data-id="528092be144b4fbf65893404" selected="selected">first-step</option><option data-id="52809373144b4fbf6589340c">kmeans</option></select></td>
                                        <td><select id="ooo"><option data-id="output" selected="selected">output</option></select></td>
                                        <td><input id="value1" class="feature-execution"value="id"></td></tr></tbody>                                        
                            </table>

Here is my solution, a function to made an json array from table

JAVASCRIPT

var jsonArray = {};

$('#development_mapping').find('tr').each(function () {
    var name = $(this).find('td:first').text(); 

    jsonArray[name] = {

          variable : $(this).find('td:eq(3)').text()

    }; 
});

I have done, but not understand, why I get " " from value of 4 column. I mean, why variable in variable is always getting " "

This is my DEMO

3 Answers 3

1

Your selector: $('#development_mapping').find('tr') is selecting all <tr> tags in the table. Even the one in the <thead>! The <thead> doesn't have <td> tags, so that's where the " " is coming from.

Try this:

$('#development_mapping').find('tbody tr').each(function () {
    var name = $(this).find('td:first').text(); 

    jsonArray[name] = {

          variable : $(this).find('td:eq(3)').text()

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

Comments

0

You are using .text() but there is no actual text in your 3rd td tag.

try this maybe?

variable : $(this).find('td:eq(3) input').val()

Comments

0

There are 2 problems with your code.

  1. Remove the surrounding <tr> in your table head definition.
  2. Use this to access input value

    variable: $(this).find('td:eq(3) > input').first().val()

$.find() will return a collection and you'll need to get the first object of the collection for further use. Plus, you'll need to search for input inside the cell, not the cell itself.

Here is the working fiddle: http://jsfiddle.net/toshkaexe/7q3KP/

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.