Using onclick to set display to block

I’m working on a simple module where you can click a button and it will set the display of a “pop-up form” to block. The form itself works just fine. I got the pop up to work with a load event listener, but now I need it to appear after a button click. This very simple action seems so difficult with hubspot, or maybe I’m just making a dumb mistake. Can you help me out?

here’s the code

<button class=“cryptoChartButton” onclick=“DisplayPopUp()” type=“button”>{% inline_rich_text field=“pop_up_button_text” value=“{{ module.pop_up_button_text }}” %}</button>

{% set href = module.link_cryptochart.url.href %}
{% if module.link_cryptochart.url.type is equalto “EMAIL_ADDRESS” %}
{% set href = “mailto:” + href %}
{% endif %}
<a href=“{{ href }}”
{% if module.link_cryptochart.open_in_new_tab %}target=“_blank”{% endif %}
{% if module.link_cryptochart.rel %}rel=“{{ module.link_cryptochart.rel }}”{% endif %}
>
{% inline_rich_text field=“link_button_text” value=“{{ module.link_button_text }}” %}
</a>
{% set no_title = True %}

{% if module.form_pop_up.form_id %}
{% form
form_key=‘{{ name }}’,
form_to_use=‘{{ module.form_pop_up.form_id }}’,
no_title=‘{{ no_title }}’,
%}
{% endif %}

<script>
function DisplayPopUp(){
document.getElementsByTagName(“form”).style.display = “block”;
}
</script>

<style media=“screen”>
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}

form{
opacity: 1;
animation-name: fadeInOpacity;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 1.5s;
background-color: rgba(220, 220, 220, 0.9);
display: none !important;
position: fixed;
z-index: 10000000000;
position: fixed;
width: auto;
max-height: 700px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

@keyframes fadeInOpacity {
0% {
opacity: 0;
}
100% {
opacity: 1;
}

</style>

I don’t think it’s a hubspot issue. I think it’s this line.

document.getElementsByTagName("form").style.display = "block";

If you wrap the form in a div and target it by id instead it’s working

<button class="cryptoChartButton" onclick="DisplayPopUp()" type="button">POPUP</button>

{% set href = module.link_cryptochart.url.href %}
{% if module.link_cryptochart.url.type is equalto "EMAIL_ADDRESS" %}
{% set href = "mailto:" + href %}
{% endif %}
<a href="{{ href }}"
{% if module.link_cryptochart.open_in_new_tab %}target="_blank"{% endif %}
{% if module.link_cryptochart.rel %}rel="{{ module.link_cryptochart.rel }}"{% endif %}
>
{% inline_rich_text field="link_button_text" value="{{ module.link_button_text }}" %}
</a>

{% set no_title = True %}

<div id="form-holder">
 HERE'S MY FORM!!!
{% form
form_key='{{ name }}',
form_to_use='{{ module.form_pop_up.form_id }}',
no_title='{{ no_title }}',
%} 
</div>

{% require_js position="footer" %}
<script>
function DisplayPopUp(){
document.getElementById("form-holder").style.display = "block";
}
</script>
{% end_require_js %}

<style media="screen">
*,
*:before,
*:after{
padding: 0;
margin: 0;
box-sizing: border-box;
}

#form-holder {
opacity: 1;
animation-name: fadeInOpacity;
animation-iteration-count: 1;
animation-timing-function: ease-in;
animation-duration: 1.5s;
background-color: rgba(220, 220, 220, 0.9);
display: none;
position: fixed;
z-index: 10000000000;
position: fixed;
width: auto;
max-height: 700px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

@keyframes fadeInOpacity {
0% {
opacity: 0;
}
100% {
opacity: 1;
}

</style>

Note: I also pulled out the conditional around the form but that’s just because I wasn’t going to create a whole module to test it :slightly_smiling_face:


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact

Thank you!

No worries!


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact