Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:34:42+00:00 2026-05-27T14:34:42+00:00

I have an a ajax method that hijacks every link in the HTML and

  • 0

I have an a ajax method that hijacks every link in the HTML and preventDefault and then loads the loadPage function().

The method works on all other links that does not have a <img> inside. But when a <a> has a <img> inside the method clickEvent.target.href does not seem to work.

The var url in this instance returns undefined in the console, but on any other links it returns the href.

I’m guessing there is something wrong with I use the target method in this instance?

$('#container a').click(function(clickEvent){
         var url = clickEvent.target.href;
         if(url.match(urlWebServer)) {
                     clickEvent.preventDefault();
                     loadPage(url);
         }
});



<div id="user_img">        
    <a href="somepage.html"><img src="img_user/self.jpg" class="self" /></a>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:34:43+00:00Added an answer on May 27, 2026 at 2:34 pm

    Yes, you shouldn’t use the clickEvent.target because is the target that was clicked. In your case the target isn’t the a but is the img it contains.

    Events in fact bubble up, so you click the image and the click event propagates to the top (up to the a) and triggers the event delegate you provided.

    The solution is to change that line to

    var url = this.href;
    

    Or if you prefer to get the value through jQuery use

    var url = $(this).attr('href');
    

    The first one is faster.

    The solution is that this in the handler refers to the DOM element you attach the handler to. So this will refer to the a.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some AJAX calls that render PartialViewResults via the jQuery.AJAX method. This works
I have a AJAX post method that gets xml data from server. But when
I am simply learing Ajax with jQuery and have a simple page method that
Lets say i have a ajax method which call a script that checks if
I have an ajax call in the head section of my index.html $(function() {
I have a ajax method that renderes a @collection of users. This code is
I have a subscriber#create method that is only used for ajax submits to it
I have added HTML to my page using the .after() method using HTML that
I have an AJAX-based WebGet method that returns JSON. It won't work with JSON
I have this code in my create.js.erb file: pollingAJAX(); function pollingAJAX() { $.ajax({ method:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.