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 8341939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:37:46+00:00 2026-06-09T05:37:46+00:00

I’ll try and explain what I’m trying to do as clearly as I can

  • 0

I’ll try and explain what I’m trying to do as clearly as I can and hopefully people’ll be able to understand the issue.

$(document).ready(function() {
  $('body').on('click', '.mainNav', function() {
    var href = $(this).attr('href');
    $('body').load(href);
   });
});

This is the bit of code which has been causing me trouble. Basically what it’s -supposed- to do is grab the ‘href’ attribute from a div and then load that to replace the current content of the body tag.

This works the first time but when the new content is generated via .load() the div stops responding. I’m pretty new to JS so I apologize in advance if the answer is something obvious. I’ve been trying to find a solution on my own but everything I find tells me that .on() should work for dynamically generated content.

EDIT: Can’t emulate the problem in a fiddle so I’m posting a pastebin with the entire index page in hopes of it clarifying the problem.

http://pastebin.com/xT5syd9j

EDIT: It works using adeneo’s solution. Much obliged to everyone for their input and apologies for not posting the entire thing from the start. I’m guessing it would’ve saved everyone some time.

  • 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-09T05:37:48+00:00Added an answer on June 9, 2026 at 5:37 am

    Not really sure what the issue it, but I’m guessing it has something to do with loading into the same element as the handler is attached to, so try :

    $(document).ready(function() {
        $(document).on('click', '.mainNav', function(e) {
            e.preventDefault();
            console.log('I just clicked : '+e.target.href);
            $('body').load(e.target.href);
        });
    });
    

    Also, you are of course aware that this will replace ALL content on your page, and unless there is a new element with the same class in the loaded content, nothing will happen. I would suggest using a container for loading the content into aswell. And of course, the links has to be to actual files on your server.

    Also make sure the files you load does not have another set of <head>, <body> tags etc. as that will mess things up for you, HTML content only!

    EDIT:

    If .mainNav is a <a href="link_you_are_trying_to_load"></a> type of element, you will have to prevent the default action as well, see updated code.

    NEW EDIT:

    The problem you are having is pretty obvious, you are using elements like :

    <div class="mainNav" href="test.php" id="active">
    

    a div element has no attribute href, so JS will not get that value as it’s not a valid attribute for anything other than a <a> element

    You can either change your element to an <a> element or change the href attribute to:

    <div class="mainNav" data-href="test.php" id="active">
    

    and do:

    var href = $(this).data('href');
    

    at least that would be valid HTML!

    Also, you seem to have a “container” like element called #site so why don’t you do :

    $('#site').load(href); //using the data-href as above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.