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

  • SEARCH
  • Home
  • 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 8769475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:21:28+00:00 2026-06-13T17:21:28+00:00

Possible Duplicate: javascript not working in the new part when loading a new part

  • 0

Possible Duplicate:
javascript not working in the new part when loading a new part using jquery and HTML 5

I loaded another HTML page into a div of the main HTML page using Ajax. The page loaded fine, but when I try to apply jQUery to the loaded page, nothing works.

$('.mainDiv').click(function(ev) {
    ev.preventDefault();
    var url = $(this).attr('href');
    $('.secondDiv').load('http://127.0.0.1:8000'+url);
});

I tried using a simple alert function called on all links to see if it works, but the alert appears only on the main page links, even though the .js file is linked to both pages.

$('a').click(function(){
    alert('hey');
});

How can I fix this? Would using .live() or .on() instead of .load() fix the problem?

  • 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-06-13T17:21:29+00:00Added an answer on June 13, 2026 at 5:21 pm

    Using $('body').on('click', 'a', function(event){...}) would work.

    Because on is called on the <body> you don’t need to worry about the event getting wiped out when you load in new content.

    If you would like to scope this event on a specific set of anchors I would suggest name spacing with a class in the second parameter using something like

    $('body').on('click', '.mainDiv .secondDiv a', function(event){...})
    

    By setting this once in a bootstrap you can reduce a lot of complexity within your code. Now all you need to do is load in the content; there is no need to define a callback inline as you’ve extracted out the event logic.

    $('a').on(...) will not work as the DOM element it’s bound to may get wiped out, or is not on the page at the time of the call. The 2nd parameter of the on call is used to reference any new elements appearing under the root element (body in this case).

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

Sidebar

Related Questions

Possible Duplicate: Css and javascript Not Loading in rewrite rule After using only rewrite
Possible Duplicate: how to display jquery page(inside a div) using javascript? here is my
Possible Duplicate: How to reload a page using Javascript? I have the Facebook login
Possible Duplicate: Difference between using var and not using var in JavaScript var foo
Possible Duplicate: When is JavaScript’s eval() not evil? I know, generally using eval() is
Possible Duplicate: Difference between using var and not using var in JavaScript For the
Possible Duplicate: .trim() in JavaScript not working in IE i have the following code
Possible Duplicate: sort not working with integers? How to sort number in javascript sort
Possible Duplicate JavaScript: Changing src-attribute of a embed-tag but this is not working for
Possible Duplicate: JavaScript alert not working in Firefox 6 I often execute Javascript code

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.