I am currently trying to toggle the visibility of a download button after the user has submitted their email. This submission is done from a regular Elementor form. Although elementor provides a range of actions, it seems like I can only toggle the visibility of the button with my own code. However, I can not hook to the correct action for some reason.
<script>
let button = document.getElementById('pdfdownloadbutton');
let form = document.getElementById('signupform');
document.addEventListener('DOMContentLoaded',()=>{
button.style.visibility = 'hidden';
});
form.addEventListener('submit',()=>{console.log("ev trig");});
</script>
I expected to log in whenever the event was called, however, the console shows an error saying that ‘addEventListener’ is not a property of null. I assume the document can not locate a form with said id, but, I did name it correctly.
Here is the part where I name the id of the form
3
Answers
A few things to note here without having much context on the rest of your code:
alert
in the submit event handler, an alert won’t do anything to show or hide your button, it will only show a browser alert with the string passed to it in the parenthesis. More on that in MDN.DOMContentLoaded
(MDN) may not do anything, it may be best to start with the button hidden, and then add a class to it to show it when appropriate. The class must be targeted by some CSS, of course.Here’s my shot in the dark at addressing the high level idea you’re expressing here:
Additionally, you’ll need some CSS for the code above to work:
You can see it in action in this Codepen.
Note that your code doesn’t give the full content so I had to fill in the blanks. Particularly, notice how I added
event.preventDefault()
(MDN) because the normal behavior of a submit event would load a new page, so you should consider if this is happening on your site in which case you may need a different solution than your current approach.Edit 1
It looks like you added additional context and an error that shows you’re selecting a
null
element.Your problem seems to be that you’re trying to select the form with:
But your form’s ID attribute is actually
roisignup00
as shown in the screenshot you shared. I advise you read through some docs forDocument.getElementById()
such as MDN here, but essentially, the parameter you pass to that method must be a valid HTML ID present in the document at the time you run the method.This means you should select your form like this:
Notice I use
const
instead oflet
because if I’m not going to reassign an identifier, there’s no reason to make it anything butconst
. Of course, some may say that’s opinion, reference here.Also, this may be applicable, but your form has an ID which may be dynamic (meaning that it may change), you should consult Elementor’s docs to figure out if it changes, because if it does your code will break. I mean that it could change from
roisignup00
toroisignup01
or such, but that’s just speculation from past experience with site builders, so don’t be too worried if you don’t find anything that confirms thatIt’s simpler than this. From Elementor options you enable the "Custom message" option, and in the message bar right at the end, you add your HTML code for the button. Example:
You can add a class to it and then with CSS you make it prettier.
In this way, the button will only appear when the message has been sent, it will be accompanied by the "Thank you" message.
View image example