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

193
Views
Overriding default behaviour of anchor tags using jquery

I am trying to override the default behaviour of an anchor tag so i can load in a web page on my server into the exisiting div, rather than a new tab or window.

so far i have:

myContainer.click(function(){                   
                    event.preventDefault();
                    $('a').click(function(){
                        var link = $(this).attr('href');
                        myContainer.load(link);
                    });
            });

In chrome i have to click the link twice before it does anything, in IE an FF it doesnt work at all and refreshes the page with the new link.

Any help is much appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Shouldn't it be just:

 $('a').click(function(e) {
     e.preventDefault(); 
     myContainer.load(this.href);
 });

Your code assigns a click handler inside a click handler. So the first click will attach the click handler to the link, and the second click (on the link) will execute the new click handler.

It seems you only need one click handler. If the links are added dynamically, you can use .on() (the successor of .live and .delegate):

myContainer.on('click', 'a', function(e) {
     e.preventDefault(); 
     myContainer.load(this.href);
});

// or

$(document).on('click', 'a', function(e) {
     e.preventDefault(); 
     myContainer.load(this.href);
});
over 4 years ago · Santiago Trujillo Report

0

you forgot to pass in event:

myContainer.click(function(event){
event.preventDefault();
 $('a').click(function(){ var link = $(this).attr('href');
 myContainer.load(link);
 });
 });

try to rearrange them:

myContainer.ready(function(){
    $('a').click(function(event){
    event.preventDefault();
    var link = $(this).attr('href');
     myContainer.load(link);
     });
     });
over 4 years ago · Santiago Trujillo 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!