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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:02:20+00:00 2026-06-15T03:02:20+00:00

I hope you can read my code, I have an XML file on my

  • 0

I hope you can read my code, I have an XML file on my localhost and I would like it to pull the title and year from that file (it currently has Title, Year, Artist, Price, and Country) while maintaining the H1 and button on the page.

The H1 text and button disappear onClick and I would like it to remain on the same page as the results.

  <h1>Show the Album list</h1>  
   <script>  
   xmlhttp=new XMLHttpRequest();  

   xmlhttp.open("GET","cd_catalog.xml",false);  
   xmlhttp.send();  
   xmlDoc=xmlhttp.responseXML;  
   function CdCatalog()  
    {
       document.write("<table border='1'><th>TITLE</th><th>YEAR</th>");
       var x=xmlDoc.getElementsByTagName("CD");  
       for (i=0;i<x.length;i++)
    {
   document.write("<tr><td>");
   document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
   document.write("</td><td>");
   document.write(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
   document.write("</td></tr>");
    }
       document.write("</table>");
    }
   </script>

  • 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-15T03:02:21+00:00Added an answer on June 15, 2026 at 3:02 am

    The problem is that you’re using document.write for a button’s click event, which is after the document’s been closed…which means it overwrites everything. Since your page is simple and only has an <h1> and button, it looks like only those things are being hidden, but it would be everything on the page (if you had more).

    The solution is to use .appendChild and/or .innerHTML to add the content dynamically. I’ll provide a solution in a minute 🙂

    UPDATE:

    Since you’re using tables, you might as well use the native methods .insertRow and .insertCell that make table creation much easier. Here’s an example of what you could use overall:

    function CdCatalog(xmlDoc) {
        var table = document.createElement("table");
        var thead = table.createTHead();    // Where "header" rows go
        // `insertRow` creates a <tr> element and appends it to `thead` automatically, returning the element
        var tr = thead.insertRow(-1);
        var td = document.createElement("th");  // No special method for creating "th" elements
        td.innerHTML = "TITLE";  // Set its inner content
        tr.appendChild(th);  // Add it to the row (which is in the header)
        td = document.createElement("td");
        td.innerHTML = "YEAR";
        tr.appendChild(td);
    
        var x = xmlDoc.getElementsByTagName("CD");
        // "tbody" is where a table's content goes, whether you do this explicitly or not
        var tbody = table.tBodies[0];
        for (var i = 0; i < x.length; i++) {
            tr = tbody.insertRow(-1);
            // `insertCell` creates a <td> element and appends it to `tr` automatically, returning the element
            td = tr.insertCell(-1);
            td.innerHTML = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
            td = tr.insertCell(-1);
            td.innerHTML = x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;
        }
        // Actually add the table to the DOM (the <body> element in this case)...you can specify where else to put it
        document.body.appendChild(table);
    }
    
    // Make sure DOM is ready for manipulation
    window.onload = function () {
        var btn = document.getElementById("button_id");  // Whatever your button is
        // Bind the "click" event for the button
        btn.onclick = function () {
            // Make your AJAX request
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", "cd_catalog.xml", false);
            xmlhttp.send();
            xmlDoc = xmlhttp.responseXML;
            // Pass the result to the function, instead of making everything global and sharing
            CdCatalog(xmlDoc);
        };
    };
    

    And unless I’m mistaken, you can’t nest <td> inside of <table>…you must nest them in <tr>…which are nested in <table>

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

Sidebar

Related Questions

I hope you can help me. I have a web service that needs to
I would like to clarify something about TimerTask. When you have the code below:
I have a simple question about sending a file (XML file) from my webapp
I have a quick and hopefully simple question that I hope someone can help
As the title suggests I would like to be able to use data from
I'm using this code to get the pixels from a bmp file. I have
In my app I have to read an XML file and display it in
I have a few questions about Grit/Git that I hope you can help me
hope someone can answer this. Here is some sample code. namespace std { #ifdef
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID

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.