0

I have the following code:

<table id="Product">
       <thead>
              <tr>
                    <th>ProductId</th>
                    <th>Productname</th>
                    <th>Quantity</th>
                    <th>UnitPrice</th>
              </tr>
       </thead>
       <tbody>

       </tbody>
</table>

<button id="add" data-table="Product" data-url="Product/Add"></button>

Then in javascript file:

     $('#add').click(function () {
            var url = $(this).attr("data-url");
            var table = $(this).attr("data-table");
            var tableBody = ???; 
            //some logic with tablebody

    });

How can i get table body of the table?

4
  • 2
    What do you mean by table body? Which table? Commented Apr 15, 2016 at 0:29
  • Please check new details Commented Apr 15, 2016 at 0:33
  • var tableBody = $("table tbody") or var tableBody = $("#" + table) What is expected result? Commented Apr 15, 2016 at 0:33
  • var tableBody = $("#"+table+" tbody"); this woks, $("table tbody") this pick any (or first) table tag... thanks Commented Apr 15, 2016 at 0:40

2 Answers 2

2
$('#add').click(function () {
    var $this = $(this);
    var url = $this.data("url");
    var tableId = $this.data("table");
    var $tableBody = $("#" + tableId + " tbody");
});

Read more about jQuery selectors at https://api.jquery.com/category/selectors/

Here I'm using the ID Selector and the Descendant Selector.

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

Comments

1
 $('#add').click(function () {
        var url = $(this).attr("data-url");
        var table = $(this).attr("data-table");
        var tableBody = $("#" + table).find("tbody"); 
        //some logic with tablebody

});

1 Comment

thanks, this also work var tableBody = $("#"+table+" tbody");

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.