tags:

views:

46

answers:

1

I am tryig to read from the textboxes in a table. How can I do it? I have the following code, but it`s not working.

<table id="dataTable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Value</th>
        </tr>
    </thead>
    <tr>
        <td>
            <input id="Name" type="text" value = "test1" />
        </td>
        <td>
            <input  type="text" value = "test11" />
        </td>
        <td>
            <input  type="button" value = "button11" />
        </td>
    </tr>
</table>

My actual script is as follows:

$(document).ready(function() {
    $("#Click").click(function() { 
        var i = 0;
        var inputs = new Array();
        $("#dataTable tr").find("input").each(function() {
            alert($(this.value));
            alert($(this.id));
            inputs[i] = [this.id, this.value];
            i++;
        });

        var columnsCount = $("#dataTable").find("th").length;
        alert($(columnsCount));
    });
});

<input type="button"  id= "Click" value="Click" />
+1  A: 

You can replace

$("#dataTable tr").find("input")

with

$("#dataTable tr input:text")

There is no value property and id property for a jQuery object.

Replace

$(this.value)

with

$(this).val()

and

$(this.id)

with

$(this).attr ( "id" )

There is no need of $(columnsCount), you can use columnsCount.

Edited your code

$(document).ready(function() {
    $("#Click").click(function() { 
        var inputs = new Array();

        $("#dataTable tr input:text").each(function() {
            inputs.push ( [this.id, this.value] );
        });

        var columnsCount = $("#dataTable th").length;
        alert(columnsCount);
    });
 });
rahul