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
You have tried to access the element by using # inside document.getElementById, which is wrong.
You can use SetTimeout function for the delay.
Working code-pen solution based on the comments – Link
not use the
#
or.
for input parameter for thedocument.getElementById()
MDN
you need to delete
#
ingetElementById('mce-success-response')