skip to Main Content

I have an email form using MailChimp and I need to delete the “Thanks for subscribing” message inside a div after 5 seconds but I can’t select the text inside the div.
I tried getElementById, querySelector, innerText, innerHTML, with and without #, but I keep getting null value.
I also tried querySelectorAll to see all the nodes and it shows the div and id as div#mce-error-response.responde and the text is inside the innerHTML

<div id="mce-responses" class="clear">
    <div class="response" id="mce-error-response" style="display:none" wfd-invisible="true"></div>
    <div class="response" id="mce-success-response" style="" wfd-invisible="true">Thank you for subscribing!</div>
</div>
let myForm = document.querySelector('#mc-embedded-subscribe-form');
let msg = document.getElementById('#mce-success-response').innerHTML;

myForm.addEventListener('submit', onSubmit);

function onSubmit(e) {
    e.preventDefault();

    console.log(msg);

That div is being added by MailChimp after clicking submit
Here’s the code https://codepen.io/m4573r00/pen/GRpwdKJ

Here’s the node
https://ibb.co/gw0jf8J

3

Answers


  1. You have tried to access the element by using # inside document.getElementById, which is wrong.

    You can use SetTimeout function for the delay.

    setTimeout(function(){
           var el = document.getElementById('mce-success-response');
           el.style.display = "none";
    }, 1000);
    

    EDIT

    Working code-pen solution based on the comments – Link

    Login or Signup to reply.
  2. let msg = document.getElementById('mce-success-response').innerHTML;
    

    not use the # or . for input parameter for the document.getElementById()
    MDN

    Login or Signup to reply.
  3. let myForm = document.querySelector('#mc-embedded-subscribe-form');
    let msg = document.getElementById('mce-success-response').innerHTML;
    
    myForm.addEventListener('submit', onSubmit);
    
    function onSubmit(e) {
        e.preventDefault();
    
        console.log(msg);
    }
    

    you need to delete # in getElementById('mce-success-response')

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search