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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:57:09+00:00 2026-06-07T19:57:09+00:00

I Wrote a code that takes a generates XML file from Harvard Uni, and

  • 0

I Wrote a code that takes a generates XML file from Harvard Uni, and put it in a dropdown, next you can choose a course from the list and it will generates a table with the course details.

     <script type="text/javascript" src="Script/jquery-1.7.2.js"></script>
     <script type="text/javascript">
     $('#button').click(function () {
         document.getElementById("span").style.visibility = "visible";
         document.getElementById("button").style.visibility = "hidden";
         $.ajax({
             type: "GET",
             url: "Harvard.aspx?field=COMPSCI",
             success: function (data) {
                 var courses = data.documentElement.getElementsByTagName("Course");
                 var options = document.createElement("select");
                 $(options).change(function () {
                     ShowCourseDetails(this);
                 });
                 for (var i = 0; i < courses.length; i++) {
                     var option = document.createElement("option");
                     option.value = $(courses[i]).find("cat_num").text();
                     option.text = $(courses[i]).find("title").text();
                     options.add(option, null);
                 }
                 document.getElementById("selectDiv").appendChild(options);
                 document.getElementById("span").style.visibility = "hidden";
             }
         });
     });
    function ShowCourseDetails(event) {
        // get the index of the selected option 
        var idx = event.selectedIndex;
        // get the value of the selected option 
        var cat_num = event.options[idx].value;
        $.ajax({
            type: "GET",
            url: "http://courses.cs50.net/api/1.0/courses?output=xml&&cat_num=" + cat_num,
            success: function (data) {
                $("#TableDiv").html(ConvertToTable(data.documentElement));
            }
        });
    }
    function ConvertToTable(targetNode) {
        targetNode = targetNode.childNodes[0];
        // first we need to create headers
        var columnCount = 2;
        var rowCount = targetNode.childNodes.length
        // name for the table
        var myTable = document.createElement("table");
        for (var i = 0; i < rowCount; i++) {
            var newRow = myTable.insertRow();
            var firstCell = newRow.insertCell();
            firstCell.innerHTML = targetNode.childNodes[i].nodeName;
            var secondCell = newRow.insertCell();
            secondCell.innerHTML = targetNode.childNodes[i].text;
        }
        // i prefer to send it as string instead of a table object
        return myTable.outerHTML;
    }
</script>

and the body:

<div id="main">
            <div class="left">
                 <input id="button" type="button" value="Get all science courses from HARVARD"/>
                 <br />
                 <span id="span" style="visibility: hidden">Downloading courses from harvard....</span>
                 <div id="selectDiv"></div>
                 <div id="TableDiv"></div>
            </div>
        </div>

and What I get in the dropdown is only “undefined” on all the rows in the dropdown, can someone can see the problem with the code I wrote?

10x alot in advance 🙂

  • 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-07T19:57:10+00:00Added an answer on June 7, 2026 at 7:57 pm

    Working jsFiddle: http://jsfiddle.net/3kXZh/44/

    Well, I found a couple of issues..

    First, I’d stay away from setting “onclick” in the HTML. You want to separate your action layer from your content layer.

    Since you’re using jQuery anyway, try this:

    $('#button').click(function() {
        /* function loadXMLDoc contents should go here */
    });
    

    And change:

    <input id="button" type="button" onclick="loadXMLDoc()" value="Get all sci..."/>
    

    To:

    <input id="button" type="button" value="Get all sci..." />
    

    To solve your immediate problem in the JavaScript, change the loadXMLDoc function from this:

    option.value = courses[i].getElementsByTagName("cat_num")[0].text;
    option.text = courses[i].getElementsByTagName("title")[0].text;
    

    to this:

    option.value = $(courses[i]).find("cat_num").text();
    option.text = $(courses[i]).find("title").text();
    

    That should be enough to get you on to creating your tables from there.

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

Sidebar

Related Questions

I wrote some code that activity starts a service to get some text from
I am attempting to write some Java code that takes an image from a
Background: I want to take some xml from one file, put it in a
I'm trying to write a piece of code that takes in a two digit
I need to write a C++ code coverage program that takes in another C++
I recently wrote code that didnt work as i would expect, it was: message
I wrote this code that compiles on Solaris gcc it works fine too for
So, here's the problem. I wrote a code that opens a div based on
Years ago, I wrote a code template that took a few simple parameters (points)
I wrote some code in VHDL that is expected to look at a rotory

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.