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

168
Vistas
Get id of div with onclick function

I currently have a onclick even on a div as follow

function copyToClipboard(e) {
  var textBox = document.getElementById(e.id);
  console.log(textBox);
  textBox.select();
  document.execCommand("copy");
}
<div class="form-inline m-2">

  <input type="text" class="form-inline" name="myvalue" id="{{ p.id }}" value="https://diccionarioespañol.com/significado/{{ p.nombre|lower }}/" readonly />
  <button onclick="copyToClipboard(this)">📋</button>

</div>

The issue is I'm unable to get the textBox to show the input data. Instead it shows null. So how do I get the id from the div or the url value sent to the clipboard

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your button is not referencing the input. Try this.

function copyToClipboard(theId) {
  var textBox = document.getElementById(theId);
  console.log(textBox);
  textBox.select();
  document.execCommand("copy");
}
<div class="form-inline m-2">

  <input type="text" class="form-inline" name="myvalue" id="{{ p.id }}" value="https://diccionarioespañol.com/significado/{{ p.nombre|lower }}/" readonly />
  <button onclick="copyToClipboard('{{ p.id }}')">📋</button>

</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you are depending on the click event, you can check the parent from which you can find the input node.

Working Fiddle

function copyToClipboard(e) {
  const input = Array.from(e.target.parentNode.children).find(node => node.nodeName === "INPUT");
  console.log(input.id);
  var textBox = document.getElementById(input.id);
  console.log(textBox);
  textBox.select();
  document.execCommand("copy");
}
<div class="form-inline m-2">
  <input type="text" class="form-inline" name="myvalue" id="p.id"
    value="https://diccionarioespañol.com/significado/{{ p.nombre|lower }}/" readonly />
  <button onclick="copyToClipboard(event)">📋</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use closest to get the parent then use the parent to query the input and then get the event targets id. I however did not use the onclick attribute. I added a selector to the button and used addEventListsner() with that selector instead passing the function as a callback into the listener.

function copyToClipboard(e) {
  const target = e.target.closest('.form-inline').querySelector('input');
  var textBox = document.getElementById(target.id);
  console.log(target);
  textBox.select();
  document.execCommand("copy");
}

document.getElementById('button').addEventListener('click', copyToClipboard)
<div class="form-inline m-2">

  <input type="text" class="form-inline" name="myvalue" id="{{ p.id }}" value="https://diccionarioespañol.com/significado/{{ p.nombre|lower }}/" readonly />
  <button id="button">📋</button>

</div>

about 4 years ago · Juan Pablo Isaza Denunciar
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