jquery - iterate through a common javascript collection? -


so have common javascript collection, in form:

function populatecollection(){     var collection = new array();     collection['a'] = 'awesome';     collection['b'] = 'better';     collection['c'] = 'cool';     return collection; } 

in file (lets it's called commonlib.js).

now i'd keep file common library, accessible several different js documents (assume library large).

what best way iterate through collection document called, jsdoc1.js?

i gave 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 in languagecollection){         alert(i);     } } <script> <body> <input type="text" name="example" id="color" onclick="dropdown()"> </body> 

but keep getting languagecollection not defined error. i'm new jscript , jquery... advice me?

thank you!

you don't include file collection. insert following statement:

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

furthermore, defining function, returns collection , no global object that. first have call function, e.g.,

var mycol = populatecollection(); 

then can access following:

for ( var in mycol ) {   if( mycol.hasownproperty( ) {     // stuff     alert( );   } } 

alternativly can change function use global variable, visible 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 way using array if object. if there no other aspects justify that, change function following:

function populatecollection(){     return {       'a': 'awesome',       'b': 'better';       'c': 'cool'     }; } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -