Interactive title in a Modal

0

Well, I have this html:

<div>
    <span class="upgrade_description"> Upgrade 1 Segunda a Sexta das 20h às 02h</span> <a href="#abrirModal" rel="modal" class="button">R$ 399</a>
</div>
<div>
    <span class="upgrade_description"> Upgrade 2 Sábados das 08h às 20h </span><a href="#abrirModal" rel="modal" class="button">R$ 299</a>
</div>
<div>
    <span class="upgrade_description">Upgrade 3 Sábados das 08h às 14h - 14h às 20h </span><a href="#abrirModal" rel="modal" class="button">R$ 199</a>
</div>
<div>
    <span> Upgrade 4 Domingo das 08h às 14h - 14h às 20h </span><a href="" rel="modal" class="button">R$ 199</a>
</div>
<div>
    <span class="upgrade_description"> Upgrade 5 Sábados das 20h às 2h  </span><a href="#abrirModal" rel="modal" class="button">R$ 199</a>
</div>

It lists some services (upgrade 1, upgrade 2, etc.) and brings in each of them a button that opens a modal containing a form. The form is the same for any of them, so I'll use the same modal.

However, I want the title (H1) of the modal to be according to the selected service.

For example: if the user clicks to open the modal of upgrade 1, the title will be Upgrade 1 , if you click the upgrade button 2 the title will be Upgrade 2 >, and so on.

Does anyone have a suggestion of how I can do this?

The modal

CSS:

.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.5);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
 transition: opacity 400ms ease-in;
pointer-events: none;
overflow:scroll; /* activa scroll da página se a modal for muito comprida */
}

.modalDialog:target {
opacity:1;
pointer-events: auto;
}

.modalDialog > div {
width: 600px;
position: relative;
margin: 5% auto;
border-radius: 10px;
background: #fff;


-moz-box-shadow: 1px 1px 30px #000;
-webkit-box-shadow: 1px 1px 30px #000;
box-shadow: 1px 1px 30px #000;
}

 .close {
background: #00B92F;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}

.close:hover { background: #2AFF2A; }

HTML (header, which will receive the title):

<div id="abrirModal" class="modalDialog">
    <a class="close" title="Fechar" href="#fecharModal">X</a>
    <div class="modal">
    <span class="title_modal"></span>
    </div>
</div>
    
asked by anonymous 26.05.2015 / 19:30

1 answer

2

You have to do this with JavaScript ...

var upgrades = document.querySelectorAll('.button');
var modal = document.querySelector('#abrirModal .title_modal');
for (var i = 0; i<upgrades.length; i++){
 upgrades[i].addEventListener('click', mudarTitulo);   
}

function mudarTitulo(){
    modal.innerHTML = this.parentNode.querySelector('span').innerHTML;
}

This code listens for clicks on each link and will replace the title that the modal shows. Each time a click is detected it looks for div parent of that a and then span which I suppose is the description you want to use.

jsFiddle: link

    
26.05.2015 / 19:58