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

179
Vistas
Javascript insertAdjacentHTML a tag with an onclick event

When a checkbox is checked I insert a new a tag to display what is selected. I add an onclick event to that a tag but when I cick on it for some reason it gives me an error that the function cannot be found.

const removeItem = (e) => {
  e.preventDefault();
  e.target.remove();
}
document.querySelectorAll('.choose').forEach(choose => {
  choose.addEventListener('change', (e) => {
    if (e.target.checked) {
      document.getElementById('selected').insertAdjacentHTML(
        "beforeend",
        `<a id="chosen-${e.target.id}" href="" onclick="removeItem()">${e.target.value}<i class="icon-close"></i></a>`
      );
    }  else {
      console.log('remove')

    }
      // document.getElementById('selected').querySelect('selectedItem').remove();
  })
})
<label>
 <input
  value="Value 1"
  id="1"
  class="choose"
  type="checkbox"
  data-np-invisible="1"
  data-np-checked="0"
 /> 
Add Value 1
</label>
<div id="selected"></div>

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

0

I tested it in the snippet and it looks like e is undefined. You need to pass event to the removeItem function here:

`<a id="chosen-${e.target.id}" href="" onclick="removeItem(event)">${e.target.value}<i class="icon-close"></i></a>`
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