Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to get text of TD in jquery on dynamically generated table in mvc4?

Hi I have one dynamically generated table. Below is the structure of dynamically generated table. I want to get value of TD which will be having class="user".

@foreach (var group in Model.detailsbyclientId)
{
    <tr class="rowid">
        <td> @group.clientName </td>
        <td> @group.employeeId </td>
        <td> @group.employeeName </td>
        <td> @group.Nationality</td>
        <td> @group.documentType </td>
        <td scope="col">
            <input type="button" class="btn btn-primary btn-cons" value="View Document" onclick="showDocumentData('@group.upld_Id');"/>
        </td>
        <td id="Hi">@group.currentStatus</td>
        <td class="user"><input type="hidden" id="Status" value="@group.currentStatus"/></td>
        <td></td>
    </tr>

I am trying to get value of each TD as below.

$(".rowid").each(function() {
    var a = $(this).find(".user").text();
    alert(a);
});

However I am getting blank in alert on each iteration. So can anyone suggest me where i am wrong in above line of code? Thank you

Upvotes: 0

Views: 275

Answers (3)

Chamith Saranga
Chamith Saranga

Reputation: 40

$(()=>{
  
   $.each($('.user'),(i,d)=>{
    
       alert($(d).html());
   
  });
  
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
      
      <tr>
          <td> 1 </td>
           <td class='user'> google </td>   
      </tr>
      <tr>
            <td> 2 </td>
           <td class='user'> fb </td>
        </tr>
        <tr>
            <td> 3 </td>
           <td class='user'> youtube </td>
        </tr>
   </table>

Upvotes: 0

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

You should get the value of input inside .user like following.

$(".rowid").each(function() {
    var a = $(this).find(".user input").val();
    alert(a);
});

Upvotes: 3

Rakesh Sojitra
Rakesh Sojitra

Reputation: 3658

try this

$(".rowid").each(function() {
        var a = $(this).find(".user").html();
        alert(a);
    });

if you are looking for value of input then

        $(".rowid").each(function () {
            var a = $(this).find(".user input").val();
            alert(a);
        });

Upvotes: 0

Related Questions