0

I want to get the row records, which is added dynamically. I am getting an output if I add the row using html.

At beginning there will be no records in table. this is my code.

HTML Code

<table class="table table-hover " id="queryTable">
    <thead>
        <tr>
           <th>Field Name</th>
           <th>Values</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

Table with empty row At beginning When user click ADD ButtonIt will be added rows using jquery successfully

After Added rows by user clicking the ADD Button JQUERY Code

$('#queryTable > tbody:last-child').append('<tr><td class="FieldNameID">' + selectedField + '</td><td class="OperatorID"> IN(' + $("#txtFilterValue").val() + ')</td></tr>');

Upto this it's working fine. Now, When user clicks on any row, I want to select the row and show it in alert box, which is added dynamcally.

3
  • You dont need :last-child Commented Feb 2, 2017 at 7:47
  • @Hemal that is not the problem now, I got the output to add the rows in table dynamically. Now I want to get the row records using jquery when user click any rows after it added Commented Feb 2, 2017 at 7:49
  • See my answer below. Commented Feb 2, 2017 at 7:49

2 Answers 2

1

WORKING FIDDLE

The elements which are added dynamically, you have to bind an event this way.

$(document).on("click",'#queryTable tbody tr',function(){
   alert($(this).html());
});

Show only td values

 $(document).on("click",'#queryTable tbody tr',function(){
   var tr=$(this);
   var firsttd=tr.find("td:first").text();
   var secondtd=tr.find("td:last").text(); //BECAUSE YOU HAVE ONLY 2 TDs
   alert(firsttd + ' ' + secondtd);
});

UPDATED FIDDLE

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

2 Comments

but it's displaying all the HTML Elements. I wants to alert only those two td values
Select as answer if it helped
0

Use a function which has a callback function. Like bellow:

function AddNewRow(selectedField,callback){
   var $newRow=$('<tr><td class="FieldNameID">' + selectedField + '</td><td class="OperatorID"> IN(' + $("#txtFilterValue").val() + ')</td></tr>');
   $('#queryTable > tbody:last-child').append($newRow);
   callback($newRow);
}

How it works:

$(document).on("click",'#selector',function(){
 var selectedField='some value';
  AddNewRow(selectedField,function($tr){
    // $tr is your new Added Tr.
    // Code will goes here
  });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.