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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:09:11+00:00 2026-06-05T00:09:11+00:00

So I have a common javascript collection, in this form: function populateCollection(){ var Collection

  • 0

So I have a common javascript collection, in this form:

function populateCollection(){
    var Collection = new Array();
    Collection['A'] = 'Awesome';
    Collection['B'] = 'Better';
    Collection['C'] = 'Cool';
    return Collection;
}

in a file (lets say it’s called CommonLib.js).

Now say I’d like to keep this file as a common library, to be accessible by several different JS documents (Assume the library is very large).

What would be the best way to iterate through this collection from a document called, say jsDoc1.js?

I gave it a shot with:

<!DOCTYPE html>
<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
function dropdown() { 
    for(var i in languageCollection){
        alert(i);
    }
}
<script>
<body>
<input type="text" name="example" id="color" onclick="dropdown()">
</body>

But I keep getting a languageCollection not defined error. I’m very new to jscript and jquery… any advice for me?

Thank you!

  • 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-05T00:09:14+00:00Added an answer on June 5, 2026 at 12:09 am

    You don’t include the file with your collection. Insert the following statement:

     <script src="/path/to/CommonLib.js"></script>
    

    Furthermore, you are only defining a function, which returns your collection and no global object for that. So you first have to call that function, e.g.,

    var myCol = populateCollection();
    

    then you can access it like the following:

    for ( var i in myCol ) {
      if( myCol.hasOwnProperty( i ) {
        // do your stuff
        alert( i );
      }
    }
    

    Alternativly you can change your function to use a global variable, which would be visible to every scope.

    function populateCollection(){
        window.myNamespace = window.myNamespace  || {};
        window.myNamespace.Collection = {};
        window.myNamespace.Collection['A'] = 'Awesome';
        window.myNamespace.Collection['B'] = 'Better';
        window.myNamespace.Collection['C'] = 'Cool';
    }
    

    By the way your are using an array as if it was an object. If there are no other aspects to justify that, change your function to the following:

    function populateCollection(){
        return {
          'A': 'Awesome',
          'B': 'Better';
          'C': 'Cool'
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some javascript: eval('var incomingJSON =' + ajaxObj.responseText); for (p in incomingJSON.common) {
I am not so strong in javascript. I have a common function that I
I have this code in a common javascript file in my asp.net project. jQuery-Lint
I have a common use case that I'd like to write a function for:
I have some common javascript functionality that I want to share across several views/pages.
I have a host javascript closure, in which i keep all the common variables
I have a every common page a.html which looks like this: <html> <head> <script
So suppose I have a JavaScript array containing a bunch of elements, but these
I have some common Javascript which I would like to execute on the event
In a web application, writing javascript is a common work, and I have tried

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.