Replace full div instead of div content

I am using the following ajax script to replace the content of a div. But this only changes the content inside that div. I want to replace the whole div.

<script>
function sendmessage()
   {

   var message_content=$("#message").val();

    $.ajax({
          type: "POST",
          url: "newmessage.php",
          dataType: "html",
          data: {message:message_content},
          success: function(data) {
                    $("#newmessage").html(data); 
              }
        });
   }
   </script>

      

Here the content of id = "newmessage" has been changed. I want to replace ... with ...

+3


source to share


2 answers


Use replaceWith



<script>
function sendmessage()
   {
   var message_content=$("#message").val();
    $.ajax({
          type: "POST",
          url: "newmessage.php",
          dataType: "html",
          data: {message:message_content},
          success: function(data) {
                    $("#newmessage").replaceWith(data); 
              }
        });
   }
   </script>

      

+4


source


Use .replaceWith()

:



$("#newmessage").replaceWith(data);

      

+1


source







All Articles