jQuery

How to remove a tag without deleting its content with jQuery

In this tutorial, we are going to see how to remove a tag without deleting its content with jQuery. Sometimes you may need to remove the parent element, a typical example is removing the anchor tag around the text. With jQuery’s unwrap() method, you can easily remove the parent element and keep the HTML content or internal text intact.
 

How to remove a tag without deleting its content with jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Remove a tag without deleting its content</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("button").click(function(){
					$("p").find("a.lien").contents().unwrap();
				});
			});
		</script>
	</head>
	<body>
		<p>Lorem ipsum dolor sit amet, consectetur adipiscing <a href="#" class="lien">this is a link</a>. In dictum, non amet consequat facilisis, augue convallis tempor dapibus.</p>
		<button type="button">Remove link</button>
	</body>
</html>
Result

Lorem ipsum dolor sit amet, consectetur adipiscing this is a link. In dictum, non amet consequat facilisis, augue convallis tempor dapibus.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *