Interactive title in a Modal

Asked

Viewed 84 times

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 will use the same modal.

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

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

Someone has a suggestion as to 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>
  • Can you put the modal code? is jQuery? in this case you can set the title as an option.

  • @Sergio The modal is generated only with Css, I will add the question the code.

1 answer

2


You have to do it 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 hears clicks on each link and will replace the heading that the modal shows. Each time a click is detected it looks for the div father of that a and then the span which I suppose is the description you want to use.

jsFiddle: http://jsfiddle.net/tvof8kkc/

  • 1

    It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.