2

I have a table with

<TABLE id="dataTable">
    <TR>
        <TD><INPUT type="checkbox" class="chk"/></TD> 
        <TD><INPUT type="text" value="data1"/></TD> 
        <TD><INPUT type="text" value="data2"/></TD>
    <TR>
        <TD><INPUT type="checkbox" class="chk"/></TD> <TD>Data 2</TD> 
        <TD><INPUT type="text"/></TD> 
        <TD><INPUT type="text"/></TD> 
</TABLE>

Say I have many rows in the table. I want to fetch the data present in each cell and use it as per my requirements.

I tried doing

$(function()
{
   $("#somebutton").click(function()
   {
      $("#dataTable").find('tr').each(function(){
      if($(this).find('input.chk').is(':checked'))
      {
         var val1 = $(this).find('td:eq(1)').html();
         var val2 = $(this).find('td:eq(2)').html();

         alert(val1);
         alert(val2);
      }
   });
 });
});

But the output I get from this is as follows

<input type="text" value="data1">
<input type="text" value="data2">

I just want the output to be given as the value present inside the Text of the cell. I tried using .val() and .value() , but it gives me error. Please guide me how to achieve this.

Thanks,

3
  • 1
    Where is the button located in the code? Commented Feb 15, 2016 at 10:13
  • @shanky try this var data1 = $(this).find('td:eq(0):input[type="text"]').html();. Let me know if it solved your issue. Commented Feb 15, 2016 at 10:16
  • 1
    @androidGenX, thanks for the help. I will upvote your reply :) Commented Feb 15, 2016 at 10:28

2 Answers 2

7

You need to access the value of the input, not the value of the td:

$(function(){
    $("#onpressofabutton").click(function(){
        var data1 = $(this).find("td:eq(0) input[type='text']").val();
        var data2 = $(this).find("td:eq(1) input[type='text']").val();
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

hey @RononDex. Thanks a lot. This is working perfect for me.
Thanks a lot. This is working perfect for me too. @RononDex
0

I have two elements in a table cell

<td>
<span>
 <input type ="checkbox"/>
 <input type ="text"/>
</span>
</td>

how to get the second element text. The id is dynamic for both the checkbox / text

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.