What is replaceWith() method?

 Posted by Rajnilari2015 on 12/19/2015 | Category: jQuery Interview questions | Views: 2159 | Points: 40
Answer:

replaceWith(content) method literally replaces the original element along with it's contents .

e.g.

<!DOCTYPE html>

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#btn1').click(function(){
$('#myspan').replaceWith('This is a <i>italic text</i>.This is another <i>italic text</i>.');
});
});
</script>
</head>
<body>
<button id="btn1">Exampleof replaceWith(Content)</button>
<span id="myspan">This is a <b>bold text</b>.This is another <b>bold text</b>.</span>

</body>
</html>


When we click on the "Exampleof replaceWith(Content)" button , the output becomes

This is a <i>italic text</i>.This is another <i>italic text</i>. 


This indicates that the original span tag along with it's contents has been replaced.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response