Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!