0

I want to add <div class=""> before <table> and also </div> after </table> dynamically with javascript. How I can do it.

2 Answers2

2

You have jQuery as a tag so assuming you already have that you can use the wrap() function.

To wrap all tables you could do

$('table').wrap('<div class=""></div>');
Dan Def
  • 1,836
  • 2
  • 21
  • 39
0

You are looking for this:

$(document).ready(function(){
    //before added
    console.log($('.main-content').html());
  
    $("button").click(function(){
       $("table").wrap("<div class='AddedTag'></div>");
       //after added
       console.log($('.main-content').html());
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <div class="main-content">
  
   <table>
    <tr>
      <th>Test table col</th>
    </tr>
  </table>
   
 </div>
 <button>Click to add</button>
AHJeebon
  • 1,218
  • 1
  • 12
  • 17