January 19, 2025
How to wrap an HTML element with div using jQuery
We can wrap an HTML element with div element by using wrap() method. We need to take HTML element as selector which needs to be wrapped and we need to pass div as argument in wrap method. Sample code is mentioned below:
Example
<html>
<head>
<title>Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<span>sample span</span>
<script>
$(document).ready(function () {
$('span').wrap('<div></div>');
});
</script>
</body>
</html>
Output
<div><span>sample span</span></div>
span element will be wrapped with div element