Chan Chun Weng
Chan Chun Weng

Reputation: 896

Jquery wrap table tbody with a div

How do I wrap the table's tbody with a div?

myTable:

<table>
    <thead></thead>
    <tbody></tbody>
    <tfoot></tfoot>
</table>

my goal:

<table>
    <thead></thead>
    <div class="scrollbar"><tbody></tbody></div>
    <tfoot></tfoot>
</table>

EDIT:

I am using jquery plugin mcustomscrollbar to make sure the tbody part stick at 30% of the screen height when the data rows increase.

Upvotes: 1

Views: 3531

Answers (3)

Beri
Beri

Reputation: 11610

Whell if you really want to do this, then use wrap :

$('table > tbody').wrap('<div class="scrollbar"></div>')

Doc: http://api.jquery.com/wrap/

But as mentioned before, you are creating a not valid HTML. I don't see the purpose for this, unless you are interested in some sort of scrolling table body. But for that I would recommend using some sort of jQuery library or css.

Upvotes: 4

prabin badyakar
prabin badyakar

Reputation: 1736

$('.scrollbar').html('<tbody></tbody>')

This will add tbody inside .scrollbar div .

Upvotes: -2

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

As per your requirement you can do this with css only:

tbody {
  display: block;
  height: 100px;/*apply your height for the tbody*/
  overflow-y: auto;
  overflow-x: hidden;
}

Upvotes: 3

Related Questions