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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:50:48+00:00 2026-06-03T01:50:48+00:00

I am currently trying to create a javascript closure that will create a drop

  • 0

I am currently trying to create a javascript closure that will create a drop down list using values from the XML file I have. I have got it working for most of the code, and was loading the XML file each time I had to use it. I want to make the code more efficient though and only load the XML file once. I’ve tried to do this using the code below, but keep getting an error.

This is the code I have for the closure…

var closure = function() {

    var xmlDoc;

    return{

            setXML: function()
            {
                xmlDoc = this.loadXMLDoc("feed.xml");
            },


            loadXMLDoc: function(dname)
            {
                if (window.XMLHttpRequest)
                {
                    xhttp=new XMLHttpRequest();
                }
                else
                {
                    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }

                xhttp.open("GET",dname, false);
                xhttp.send();
                return xhttp.responseXML;
            },


            createField: function() 
            {
                x = xmlDoc.getElementsByTagName("name");
                //code to create the drop down box
            },

            findValue: function() 
            {
                y = xmlDoc.getElementsByTagName("value");
                //code to find value
            }

   };

}();

This is the code that calls the function…

$(document).ready(function () {
    closure.setXML();
});

The error i’m getting is ‘Cannot call method ‘getElementsByTagName’ of undefined’, which is found in the createField function. It’s the first instance of me trying to use the xmlDoc.

Any help or tutorials on the issue is much appreciated.

  • 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-03T01:50:49+00:00Added an answer on June 3, 2026 at 1:50 am

    Since you are doint jQuery, why not use jQuery in your closure also? If xmlDoc was undefined, there must be issues loading it. Try doing a console.log to see if any data was returned after doing AJAX.

    var closure = (function($) {
    
        var xmlDoc;
    
        return {
            setXML: function() {
                this.loadXMLDoc("feed.xml", function(data) {
                    xmlDoc = data;
                });
            },
            loadXMLDoc: function(dname, callback) {
                $.get(dname, function(data) {
    
                    //a possible point of failure
                    //do console.log(data) to see if anything was returned
    
                    callback(data);
                }, 'xml');
            },
            createField: function() {
                x = $("name",xmlDoc);
                //code to create the drop down box
            },
            findValue: function() {
                y = $("value",xmlDoc);
                //code to find value
            }
        };
    }(jQuery));​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create javascript closure that will tell me if the function has
I'm currently trying to create a kernel module that will produce data based on
I am currently trying to create an Android application that loops Audio from the
I'm currently trying create a service that will return results of a OLAP cube
I am currently trying to pass an array that I have created in Javascript
I am currently trying to create a win32 service using pywin32. My main point
I'm currently trying to create a program that estimates location based on signal strength.
I am currently trying to create a function which will allow me to pass
I'm currently trying to create a desktop like homepage where using can move around
I am currently trying to create a test suite for my javascript apps. My

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.