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

Mohammed Abu Faysal
- 151
- 2
- 5
2 Answers
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
-
but need one ' end of div tag – Mohammed Abu Faysal Oct 16 '16 at 11:07
-
@MohammedAbuFaysal ah yes you're right, have fixed my answer, thanks. – Dan Def Oct 16 '16 at 11:15
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