# Modal window from image button

**URL:** <https://community.hubspot.com/t/modal-window-from-image-button/35592>\
**Category:** CMS Development\
**Created:** [April 15, 2021, 4:27pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592 "2021-04-15T16:27:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JCaulcutt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jcaulcutt/32/2679_2.png) [@JCaulcutt](https://community.hubspot.com/u/JCaulcutt)\
**Post date:** [April 15, 2021, 4:27pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/1 "2021-04-15T16:27:52Z")

</div>

I’m trying to create a custom module that has a button item contain a logo, modal pop-up description, logo name field. I can get the modal pop-up to work when there is no “item” but as soon as I create the item to group the fields, it breaks.

The reason I’m putting it in a grouped item is because I need to put multiple logos with modals on a page. (see attached for what I’m trying to take their static images and place modals over [https://www.mbassett.com/our-clients)](https://www.mbassett.com/our-clients)

This is the code before putting it in grouped items (which works):

\<!-- The button --\>  
\<a href=“#{{ widget.modal\_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“open-popup-link”\>  
\<img src=“{{ widget.logo.src }}” alt=“{{ widget.logo.alt }}”\>  
\</a\>

\<!-- the modal popup is hidden until the button is clicked --\>  
\<div id=“{{ widget.modal\_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“white-popup mfp-hide”\>  
{{ widget.logo\_name }}  
{{ widget.modal\_popup }}  
\</div\>

\<!-- Copy and paste your css file between the quotations in the href below --\>  
\<link rel=“stylesheet” href=“CSS\_File\_URL\_Goes\_Here”\>  
\<style\>  
.white-popup {  
position: relative;  
background: #FFF;  
padding: 20px;  
width: auto;  
max-width: 500px;  
margin: 20px auto;  
}  
\</style\>

\<!-- Copy and paste your js file between the quotations in the src below --\>  
\<script src=“{{ get\_asset\_url(‘/Martyn\_Bassett\_November2018 Theme/Coded Files/Logo PopUp Modal.js’) }}”\>\</script\>  
\<script\> $(document).ready(function() {  
$(‘.open-popup-link’).magnificPopup({  
type:‘inline’,  
midClick: true  
});  
});  
\</script\>

This is the code I’m trying to get to work for grouped item “logo\_item”:

\<!-- The button --\>  
\<a href=“#{{ widget.item.modal\_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“open-popup-link”\>  
\<img src=“{{ widget.item.logo.src }}” alt=“{{ widget.item.logo.alt }}”\>  
\</a\>

\<!-- the modal popup is hidden until the button is clicked --\>  
\<div id=“{{ widget.item.modal\_button|replace(‘&’,‘and’)|replace(’ ‘,’-‘)|replace(’(‘,’-‘)|replace(’)‘,’-‘)|replace(’?‘,’‘)|replace(’”‘,’‘)|replace("’“,”“)|replace(”.“,”“)|replace(”:“,”“)|replace(”,“,”“) }}” class=“white-popup mfp-hide”\>  
{{ item.logo\_name }}  
{{ item.modal\_popup }}  
\</div\>

\<!-- Copy and paste your css file between the quotations in the href below --\>  
\<link rel=“stylesheet” href=“CSS\_File\_URL\_Goes\_Here”\>  
\<style\>  
.white-popup {  
position: relative;  
background: #FFF;  
padding: 20px;  
width: auto;  
max-width: 500px;  
margin: 20px auto;  
}  
\</style\>

\<!-- Copy and paste your js file between the quotations in the src below --\>  
\<script src=“{{ get\_asset\_url(‘/Martyn\_Bassett\_November2018 Theme/Coded Files/Logo PopUp Modal.js’) }}”\>\</script\>  
\<script\> $(document).ready(function() {  
$(‘.open-popup-link’).magnificPopup({  
type:‘inline’,  
midClick: true  
});  
});  
\</script\>

Can someone please tell me what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [April 16, 2021, 3:08pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/2 "2021-04-16T15:08:01Z")

</div>

@Kevin-C

any words of wisdom for @JCaulcutt ?

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [April 19, 2021, 1:47pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/3 "2021-04-19T13:47:55Z")

</div>

Thanks @dennisedson

Hey @JCaulcutt

I wonder how you’re HUBL is actually outputting the HTML? And then how you’ve modified you JS to match?

One possible approach:

Assuming the HUBL is generating a modal for every button. We will need to pass magnificPopup the ID of the modal element we want to open. See the [documentation for inline elements](https://dimsemenov.com/plugins/magnific-popup/documentation.html#inline-type) here.

`EDIT: WAS BAD JS`

This is assuming that ID is unique!

If that is not the case we 'll need to come up with a way to make it so.

---

<div class="post-metadata">

**Author:** ![JCaulcutt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jcaulcutt/32/2679_2.png) [@JCaulcutt](https://community.hubspot.com/u/JCaulcutt)\
**Post date:** [April 19, 2021, 7:42pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/4 "2021-04-19T19:42:34Z")

</div>

This works when i have a one-time use of the grouped fields but as soon as I put the grouped fields to “repeater options” turned on then it doesn’t work. I feel like I’m not naming the fields properly when they’re repeated. I’m including my code below as well as the screen shot of field items in the group.

HTML:

\<a href=“#test-popup” class=“open-popup-link”\>\<img src=“{{ widget.item.logo.src }}” alt=“{{ widget.item.logo.alt }}”\>\</a\>

\<!-- Popup itself --\>  
\<div id=“test-popup” class=“white-popup mfp-hide”\>  
Popup content  
\</div\>

CSS:

.white-popup {  
position: relative;  
background: #FFF;  
padding: 20px;  
width:auto;  
max-width: 500px;  
margin: 20px auto;  
}

JS:

$(‘.open-popup-link’).magnificPopup({  
type:‘inline’,  
midClick: true // allow opening popup on middle mouse click. Always set it to true if you don’t provide alternative source.  
});

 ![41164i54E6A7E00F01EF24.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/2/5/25d41ec876e97d05b8dbd4c91bedab68626e80b7.png)

for them to show the image as a button and then trigger the modal.

Any solutions for this?

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [April 19, 2021, 8:36pm UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/5 "2021-04-19T20:36:50Z")

</div>

Ohhh my bad @JCaulcutt

You’re loop should look something like this. Note how we access the properies of each “item”:

```lia-code-sample
<!-- HTML & HUBL -->

{% for item in module.logo_item %}
 <img data-id ="{{ '#test-popup' ~ loop.index }}" class="test-img" src="{{ item.logo.src }}" alt="{{ item.logo.alt }}" width="100" />

 <!-- Popup itself -->
	<div id="{{ 'test-popup' ~ loop.index }}" class="white-popup mfp-hide">
		{{ item.modal_popup }}
	</div>
{% endfor %}

```

```lia-code-sample
/* CSS */

.test-img {
 cursor: pointer;
}​

```

```lia-code-sample
// JS

$('.test-img').each(function(index, el) {
 $(this).magnificPopup({
 items: {
 src: $(this).data('id'),
 type: 'inline',
 midClick: true
 }
 });
});

```

This is kind of a down and dirty implementation that could use cleaned up.

---

<div class="post-metadata">

**Author:** ![JCaulcutt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jcaulcutt/32/2679_2.png) [@JCaulcutt](https://community.hubspot.com/u/JCaulcutt)\
**Post date:** [April 20, 2021, 12:39am UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/6 "2021-04-20T00:39:38Z")

</div>

This works when i preview from the modal. As soon as I try and put it into a page then the fields (“modal\_popup” and “logo\_name”) within the pop-up don’t appear, only the modal box appears.

Do you know why it would be different from module code to actual page preview?

 ![41189iCC5383123178D91A.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/3/c36817bd6b3954b965723486aad290c018c775ad.png)

HTML:

\<!-- HTML & HUBL --\>

{% for item in module.logo\_item %}  
\<img data-id =“{{ ‘#test-popup’ ~ loop.index }}” class=“test-img” src=“{{ item.logo.src }}” alt=“{{ item.logo.alt }}” width=“190” /\>

\<!-- Popup itself --\>  
\<div id=“{{ ‘test-popup’ ~ loop.index }}” class=“white-popup mfp-hide”\>  
{{ item.logo\_name }}  
{{ item.modal\_popup }}  
\</div\>  
{% endfor %}

CSS:

.white-popup {  
position: relative;  
background: #FFF;  
padding: 20px;  
width:auto;  
max-width: 500px;  
margin: 20px auto;  
}

.test-img {  
cursor: pointer;  
}

JS:

$(‘.test-img’).each(function(index, el) {  
$(this).magnificPopup({  
items: {  
src: $(this).data(‘id’),  
type: ‘inline’,  
midClick: true  
}  
});  
});

---

<div class="post-metadata">

**Author:** ![JCaulcutt](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jcaulcutt/32/2679_2.png) [@JCaulcutt](https://community.hubspot.com/u/JCaulcutt)\
**Post date:** [April 20, 2021, 12:58am UTC](https://community.hubspot.com/t/modal-window-from-image-button/35592/7 "2021-04-20T00:58:58Z")

</div>

@Kevin-C This actually ended up working. I realized I had made a small mistake but your solution worked perfectly for me.

Thanks for your help!
