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

189
Vistas
JavaScript: onclick function to change another elements onclick

Intended Functionality

I'm looking for something that operates like this:

  1. User clicks on the img
  2. handclick function gets called and 'enables' the parent anchor of the img
  3. If clicked again, the anchor will redirect the user to a new page

My Issue:

The below does 'enable' the link, but it also acts as if the link was clicked at the same time.

Is there any way to fix this functionality?

HTML Code:

<a href="@Url.Action("Index/" + @card.ID)" onclick="return false;" class="link">
    <img src="~/Resources/@card.Image" id="@card.ID" onclick="handClick(@((int)card.Value), @card.ID)" class="card" />
</a>

JavaScript Code

function handClick(cardValue, cardID) {
    *... irrelevant code ...*
    var playedCard = document.getElementById(cardID);
    *... irrelevant code ...*
    playedCard.parentElement.onclick = function () { return true };
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Since I don't know what the irrelevant code is I'll stay only on the first click enable issue.

My approach includes the href attribute. without it the link doesn't work. So, what I did was change the attribute's name into something else and when clicked, replace it with the actual attribute, thus "activating" the link.

Notice that the cursor changes into a pointer after the first click.

document.querySelectorAll('a[hrefx]').forEach(function(el) {
  el.addEventListener('click', function() {
    // This condition enable re-using the listener for other purposes.
    if (this.getAttribute('hrefx')) { 
      this.setAttribute('href', this.getAttribute('hrefx'));
      this.removeAttribute('hrefx');
    }
  });
});
<a hrefx="#">
    <img src="https://picsum.photos/200">
</a>
<a hrefx="#">
    <img src="https://picsum.photos/200">
</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