Replace clicked element and keep its text

Description

The following code shows how to replace clicked element and keep its text.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {<!--   ww  w  .ja  v  a  2s .c o m-->
    $("button").click(function() {
      $(this).replaceWith("<div>" + $(this).text() + "</div>");
    });
  });
</script>
</head>
<body>
  <button>First java2s.com</button>
</body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities