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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:13:32+00:00 2026-06-05T18:13:32+00:00

I have the following jquery script: $(function(){ $(‘#top-menu’).on(‘click’, ‘a.change-menu’, function(e){ e.preventDefault() $(#menu-change-div).load($(this).attr(href)); }); });

  • 0

I have the following jquery script:

$(function(){   
$('#top-menu').on('click', 'a.change-menu', function(e){   
e.preventDefault()   
$("#menu-change-div").load($(this).attr("href"));   

});  
}); 

$(function(){   
$('#menu-change-div').on('click', 'a.change-content', function(e){     
e.preventDefault()   
$("#content").load($(this).attr("href"));     
});  
}); 

Here’s the HTML inside the body:

<div id="menu-div">
<div id="top-menu">
<h3 id="top-tabs"><a href="contentColumn.html" class="change-menu">Contents</a></h3>
<h3 id="top-tabs"><a href="indexColumn.html" class="change-menu">Index</a></h3>
<h3 id="top-tabs"><a href="searchColumn.html" class="change-menu">Search</a></h3>
</div>
<div id="menu-change-div">
<ul>
//Lots of nested list items and ul's with #change-content id
</ul>
</div>
</div>

<div id="content">
<div id = "page-content">
//content section
</div>
</div>

The first function occurs when I click a list item in my menu div and loads an html file into my content div.

The second one changes whats in my menu div when a different tab is selected.

I can click list item and everything works as expected and when I click the tabs it also works properly. My problem is occuring if I first click a list item, then click a tab, then click another list item. Instead of just loading the html into the div like it did on the first click, it instead just follows the href and opens it in another window. Any ideas what I’m doing wrong?

  • 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-05T18:13:34+00:00Added an answer on June 5, 2026 at 6:13 pm

    Try:

    $(function() {
        $('body').on('click', 'a.change-content', function(e) {
            e.preventDefault()
            $("#content").load($(this).attr("href"));
            return false;
        });
        $('body').on('click', 'a.change-menu', function(e) {
            e.preventDefault()
            $("#menu-change-div").load($(this).attr("href"));
        });
    });​
    

    if elements are being added dynamically, then you need to change the way you’re using .on().

    Per the docs:

    Event handlers are bound only to the currently selected elements; they
    must exist on the page at the time your code makes the call to .on().
    To ensure the elements are present and can be selected, perform event
    binding inside a document ready handler for elements that are in the
    HTML markup on the page. If new HTML is being injected into the page,
    select the elements and attach event handlers after the new HTML is
    placed into the page. Or, use delegated events to attach an event
    handler, as described next.

    Also note that you can combine your two $(function () {...}) into one.

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

Sidebar

Related Questions

i have the following jquery: <script type=text/javascript src=./js/jquery-1.7.2.min.js></script> <script type=text/javascript> $(document).ready(function() { $(.indexSlideMenu1_Folder div).click(function()
I have the following jQuery script... $(function() { $('.eventWrapper').bind('click', function(event) { var srvName =
I have the following jquery script that I want to work <?php $script=<<<SCRIPT $(#btn).click(function(){
I have the following jquery function > <script type=text/javascript> > > $(document).ready(function() { >
I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =
I have the following KnockoutJS template (render with jquery.tmpl): <script id=contactsTemplate type=text/html> <li data-bind=click:
I have the following jQuery script: $(document).ready(function() { $.ajax({ url: 'temp.ajax.php', type: 'GET', success:
I have the following code: jQuery('#hexVal').change(function() { var hexvarval = jQuery('#hexVal').val(); jQuery('#thumbnail_hover_color').val(hexvarval); }); Basically,
i have the following Jquery function inside my view:- <script type=text/javascript> $(document).ready(function () {
I have the following jQuery code to execute when I click a button $('#substat').click(function()

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.