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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:34:27+00:00 2026-06-01T05:34:27+00:00

I have this function that switches the HTML contents from one element on a

  • 0

I have this function that switches the HTML contents from one element on a page to the another. The issue I am having is the when first iteration of this function is ran it keeps the original HTML values. I thought the below code would simply overwrite the current contents. First am I wrong?

Second, I am using Ajax to call content. Could this be the cause where some how the DOM doesn’t see the change?

Here is my code:

function subNavContent ( ) {    
    //If ID is the reference.
    //navContent = document.getElementByID('pageNav').innerHTML;
    navContent = document.getElementsByClassName('pageNav')[0].innerHTML;   
    document.getElementById('subNav').innerHTML = navContent;
    return;}

The HTML elements I call to be switched:

            <nav id="subNav" class="aniSubNavOpen drop-shadow lifted">
            </nav>

            <div class="pageNav">
            <h1 data-title="Welcome to The Mind Company">
            <a>Welcome to The Mind Company</a></h1> 
            </div>

Ajax call (and other stuff for sake of completeness.)

function subNavContent ( ) {    
    navContent = document.getElementsByClassName('pageNav')[0].innerHTML;   

    document.getElementById('subNav').innerHTML = navContent;}

function subNavLoader ( ) {
    var subNav = document.getElementById( 'subNav' );
    var tmp = '';

    if (subNav.className === 'aniSubNavClose' ) {
            tmp = 'aniSubNavOpen';  }
        else {
            tmp = 'aniSubNavClose';}

        subNav.className = tmp;
        return;}

function sectionAssure( classID, type ) {
    subNavLoader ( );

    setTimeout( function ( ) {

        var tmp = '';
        var sel = document.getElementsByTagName('section');

        for (var i=0; i<sel.length; i++){

            if (sel[i].id == classID) { tmp = 'block' } else { tmp = 'none' }
            sel[i].style.display = tmp;  }  

        subNavLoader ( ); }, ( 1500 ) );  }

function loadContent ( classID, url, type ) {   
    var xmlhttp;

    if ( window.XMLHttpRequest ) {
        xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
                document.getElementById( classID ).innerHTML=xmlhttp.responseText;}};   

        xmlhttp.open( "GET", url, true );           
        xmlhttp.send( );            
        subNavContent ( );}  
    return;  }
  • 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-01T05:34:28+00:00Added an answer on June 1, 2026 at 5:34 am

    If your objects with class="pageNav" and id="subNav" exist and pageNav has the desired content in it the first time you run the subNavContent() function, then the contents of the first object with class="pageNav" will be copied to the object with id="subNav". Note – this is not moving contents, but making a copy of the HTML and assigning it to the object subNav. It will replace the previous contents of subNav.

    If it is not working the first time you call it, then it’s probably because one of the two objects doesn’t exist yet or the contents of pageNav is not yet there when you run the function.

    In addition, you cannot have two elements with the same id. Your HTML shows two elements with id="subNav". There should only ever be one element with a given ID. That’s why document.getElementByid() only returns one element.

    Also, the HTML in your question doesn’t show any object with a class="pageNav".

    It is also possible that there is an issue with how you are making the ajax call and calling this function after the ajax call has completed. You will have to show us your ajax code for us to comment on that. If, for example, you weren’t waiting until the success handler of the ajax call gets called, then the new content wouldn’t be in the page yet the first time you ran the function.


    EDIT: after looking at the actual code for the ajax call in the OP’s actual page.

    I can confirm that the way you are calling subNavContent() in your ajax call is incorrect. This code will not work because you call subNavContent() before the new content has been loaded into the page. This is your existing version of the code:

    function loadContent ( classID, url, type ) {   
        var xmlhttp;
        var dir = getDir ( type );
    
        if ( window.XMLHttpRequest ) {
            xmlhttp = new XMLHttpRequest();
    
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
                    document.getElementById( classID ).innerHTML=xmlhttp.responseText;}};   
    
            xmlhttp.open( "GET", dir + url, true );             
            xmlhttp.send( );
    
            subNavContent ( );}  
        return;  }
    

    You are calling subNavContent() right after sending your ajax request. You can only run this call AFTER the response has been received and the data has been put in your page. You could change it to this:

    function loadContent ( classID, url, type ) {   
        var xmlhttp;
        var dir = getDir ( type );
    
        if ( window.XMLHttpRequest ) {
            xmlhttp = new XMLHttpRequest();
    
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
                    document.getElementById( classID ).innerHTML=xmlhttp.responseText;
                    subNavContent ( );
                }
            }   
    
            xmlhttp.open( "GET", dir + url, true );             
            xmlhttp.send( );
    
        }  
        return;  
    }
    

    Also, you have an extremely difficult bracing style to read, comprehend, prevent making mistakes and to edit.

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

Sidebar

Related Questions

I have this function in jQuery that gets data from a page with POST,
I have this function called ProperCase that takes a string, then converts the first
I have this function to edit all fields that come from the form and
I have a script that switches background image on hover: $('div#example').hover(function(){ $(this).css('background',url('images/bg_2.png') no-repeat bottom);
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that takes the input of a, runs a calculation and
So I have this function that forks N number of child processes. However it
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this function in a Code Igniter model that creates a new video.

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.