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

273
Visualizações
Trying to access getElementById twice, but not working... but I've read I can, so something else must be wrong

I'm not well versed in JavaScript, so I'm probably not explaining this right, but I have a jsfiddle. I have a script that pops up a modal with another page in it. If I'm using one link all is well and everything works. It's perfect for what I need.

There are times when I want to add a second link with the same ID so that it pops up with the same functionality, same page and everything, but when I do that only the first link on the page works and the second link fails.

If I remove the first link, the second one works fine.

I would appreciate any thoughts on how to fix this so they both work.

HTML:

<p>
  <a href="" id="link"
    >Add your thoughts (this works, but the second link doesn't unless this
    one is removed).</a
  >
</p>
<a href="" id="link">View or Add Comments</a><br />
<div id="popup"><iframe id="popupiframe"></iframe></div>
<div id="popupdarkbg"></div>
<p>(Click background to exit popup)</p>

JavaScript:

     document.getElementById("link").onclick = function (e) {
    e.preventDefault();
    document.getElementById("popupiframe").src = "http://www.blankwebsite.com/"
    document.getElementById("popup").style.display = "block";
    document.getElementById("popupiframe").src =
      "http://www.blankwebsite.com/";

  document.getElementById("popupdarkbg").onclick = function () {
    document.getElementById("popup").style.display = "none";
    document.getElementById("popupdarkbg").style.display = "none";
  };

CSS:

  #comments {
    width: 100%;
    text-align: center;
    margin: 15px 0 15px 0;
  }

  #popup {
    display: none;
    position: fixed;
    top: 12%;
    left: 15%;
    width: 70%;
    height: 80%;
    background-color: white;
    z-index: 10;
  }

  #popup iframe {
    width: 100%;
    height: 100%;
    border: 0;
  }

  #popupdarkbg {
    position: fixed;
    z-index: 5;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.75);
    display: none;
  }
about 4 years ago · Juan Pablo Isaza
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