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

44
Views
Jquery UI click on link

I have some links within div. If users click any area in the div, it should do something. But it should do something else if users click on a link.

How could I find out if the click is on a link? BTW, I cannot use onclick() function on the link.

<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
  </head>
  <body>
    <div class="mydiv">
      do something<br />
      <a href="#"> do other</a><br />
      do something<br />
    </div>

    <script>
      $(document).ready(function () {
        $('.mydiv').click(function () {
          var ahref = $(this).attr('href');
          if (ahref == null) {
            alert('do something');
          } else {
            alert('do other');
          }
        });
      });
    </script>
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the nodeName property to check whether the clicked element is having an a tag or not.

<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
  </head>
  <body>
    <div class="mydiv">
      do something<br />
      <a href="#"> do other</a><br />
      do something<br />
    </div>

    <script>
      $(document).ready(function () {
        $('.mydiv').click(function (e) {
          if (e.target.nodeName == 'A') {
            alert('do other');
          } else {
            alert('do something');
          }
        });
      });
    </script>
  </body>
</html>

https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName

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!