Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda