Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
javascript open and close window

i tried to make a function to open and close a rules window and i don't know why but it doesn't work, i tried to search for examples to the same idea but however i try it wouldn't work, and yeah that's it please if some one know leave a comment

const openRulesBtn = document.getElementById("openRulesBtn");
const rulesWindow = document.getElementById("bgRules");
const closeRulesBtn = document.getElementById("rulesCancelBtn");

openRulesBtn.onclick = openRules();
closeRulesBtn.onclick = closeRules();

function openRules() {
  rulesWindow.style.display = "block";
}

function closeRules() {
  rulesWindow.style.display = "none";
}
.openRulesBtn {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 7rem;
  height: 1rem;
  margin: 2rem 4rem;
  border: 2px solid hsl(217, 16%, 45%);
  border-radius: 8px;
  text-transform: uppercase;
}

.openRulesBtn:hover {
  cursor: pointer;
}

#bgRules {
  position: absolute;
  top: 50%;
  left: 50%;
  display: none;
}

#rulesWindow {
  position: relative;
  z-index: 10;
  background-color: white;
}

#rulesCancelBtn {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 11;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid rgba(0, 0, 0, 0.3);
  cursor: pointer;
}

#rulesCancelBtn:hover {
  background-color: rgb(255, 30, 0);
}
<div id="bgRules">
  <img id="rulesWindow" src="/images/image-rules.svg" alt="" />
  <div id="rulesCancelBtn">x</div>
</div>
<footer>
  <div class="openRulesBtn" id="openRulesBtn">Rules</div>
</footer>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

functionName() executes/calls the function. To add event listeners do this :

openRulesBtn.onclick = openRules;
closeRulesBtn.onclick = closeRules;

const openRulesBtn = document.getElementById("openRulesBtn");
const rulesWindow = document.getElementById("bgRules");
const closeRulesBtn = document.getElementById("rulesCancelBtn");

openRulesBtn.onclick = openRules;
closeRulesBtn.onclick = closeRules;

function openRules() {
    rulesWindow.style.display = "block";
}
function closeRules() {
    rulesWindow.style.display = "none";
}
.openRulesBtn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 7rem;
    height: 1rem;
    margin: 2rem 4rem;
    border: 2px solid hsl(217, 16%, 45%);
    border-radius: 8px;
    text-transform: uppercase;
}

.openRulesBtn:hover {
    cursor: pointer;
}

#bgRules {
    position: absolute;
    top: 50%;
    left: 50%;
    display: none;
}

#rulesWindow {
    position: relative;
    z-index: 10;
    background-color: white;
}

#rulesCancelBtn {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 11;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid rgba(0, 0, 0, 0.3);
    cursor: pointer;
}

#rulesCancelBtn:hover {
    background-color: rgb(255, 30, 0);
}
<div id="bgRules">
  <img id="rulesWindow" src="/images/image-rules.svg" alt="" />
  <div id="rulesCancelBtn">x</div>
</div>
<footer>
  <div class="openRulesBtn" id="openRulesBtn">Rules</div>
</footer>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda