Andy Newman
Andy Newman

Reputation: 63

removeChild() not working

I want to display a message once the form has been submitted and remove the form from the page.

The first part of the script is working, but the second one (where I try to remove the form) does not.

Any suggestions?

<script>
$(document).ready(function () {
    if(window.location.href.indexOf("footer") > -1) {
       document.getElementById("thanksForMessage").innerHTML = "<h2>Thanks for the message. I will contact you shortly.</h2>";
       var formDiv = document.getElementById("formwell");
       var childForm = document.getElementsByTagName("form");
       formDiv.removeChild(childForm);
    }
});

</script>

Upvotes: 1

Views: 912

Answers (2)

Abdallh Abukhader
Abdallh Abukhader

Reputation: 50

try to use something like this code

$(document).ready(function () {
if(window.location.href.indexOf("footer") > -1) {
   $("#thanksForMessage").html("<h2>Thanks for the message. I will contact you shortly.</h2>");
   var formDiv = $("#formwell");
   var childForm = "form";
   formDiv.remove(childForm);
}

});

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

   var childForm = document.getElementsByTagName("form");

This returns a NodeList (like an array), not a single element. removeChild expects a single element. You can pass just the first one by:

   formDiv.removeChild(childForm[0]);

Upvotes: 2

Related Questions