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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:04:08+00:00 2026-06-07T17:04:08+00:00

I’m trying to prevent default links, because I’m dynamically loading pages into divs with

  • 0

I’m trying to prevent default links, because I’m dynamically loading pages into divs with jQuery, and the href for each like is just the name before the page (e.g. href=home, and fixing to load to home.php in code below).

//initialize home page as active on load up and deactivate link
var activePage = $('a[href$="home"]');
var activePageHref = activePage.attr('href');
activePage.attr('href', '#');

$('a').click(function(e) {
    e.preventDefault();
    //set the page clicked on
    var page = $(this).attr('href');
    //check to see if there are any extras to url for php
    var pageCheck = page.split('?');   
    //sort page type
    if (pageType == null) {
        //dynamically load the page in div bodycontent
        $('#bodycontent').load(page + '.php');
    } else {
        $('#bodycontent').load(page + '.php?' + pageCheck[1]);
    }
    //pull up last disabled link for navigation
    var setOld = $('a[href$="' + activePageHref + '"]');
    setOld.attr('href', '' + activePageHref + '');
    //set the new disabled link for navigation
    activePage = page;
    //make link inactive
    $(this).attr('href', '#');
});

It was ok with return false at the end until I added MORE things I needed to happen in the click event function, but to be exact, this part:

//pull up last disabled link for navigation
    var setOld = $('a[href$="' + activePageHref + '"]');
    setOld.attr('href', '' + activePageHref + '');
    //set the new disabled link for navigation
    activePage = page;
    //make link inactive
    $(this).attr('href', '#');

Now the e.preventDefault();, which is from what I understand the correct way of doing what I need to happen, is stopping the entire thing from firing on any links. I’m stuck. I just need to stop default action, and use the function I’ve built with the extras at the end I’ve added to make my navigation pretty.

Also to add, I do have a hover function tied to the ul of this navigation, but didn’t include it as it shouldn’t causing an issue, but I can put it in here if needed. That is the only other thing in this document ready function.

  • 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-07T17:04:09+00:00Added an answer on June 7, 2026 at 5:04 pm

    Updated:

    Since you change the disabled link’s href initially, the attribute-contains selector won’t be able to find it again later on to activate it. I would suggest doing this a little bit differently.

    You can capture the entire ‘disabled link’ functionality by using classes. If you add a class to links which should be “disabled” you can prevent the browser from following only links with that specified class.

    When you click on an “enabled link”, follow it then make it disabled. Then, enable all other links.

    $('a').click(function() {
        $(this).addClass('disabledLink');
        $('a').not(this).removeClass('disabledLink');
    }
    

    Then, set up an event listener for the whole document which prevents the default action on links with a certain class.

    $(document).on('click', 'a.disabledLink', function(e) {
        e.preventDefault();
    }
    

    This should achieve what you want as is (i.e. it would replace your entire ‘click’ handler above). Note with this the href will never be changed to ‘#’.


    Otherwise: just cache the link itself along with its href

    //pull up last disabled link for navigation
    activePage.attr('href', '' + activePageHref + '');
    
    //set the new disabled link for navigation
    activePage = $(this);
    activePageHref = $(this).attr('href');
    
    //make link inactive
    $(this).attr('href', '#');
    

    You have a syntax error which is causing the JS engine to halt:

    //pull up last disabled link for navigation
    $('a[href$="' + activePageHref + '"]').attr('href', '' + activePageHref + '');
    
    //set the new disabled link for navigation
    activePage = page.attr('href');
    /* THIS HERE RETURNS A STRING SO YOU CHANGE activePage to a string. */
    
    // Should probably be:
    activePageHref = page.attr('href');
    
    //make link inactive
    activePage.attr('href', '#');
    

    Befor you invoke this method you set activePage to be a string, which has no method .attr() so it throws an error and execution of the function stops.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.