Thursday, April 29, 2010

Undefined properties and showing all properties for Javascript/jQuery objects

To check if a Javascript/jQuery variable has been set use the following code:

if(typeof(myVar) !== 'undefined');

Solution source: http://quomon.com/question-how-to-check-if-a-javascript-variable-is-defined-891.aspx


To get all properties of a Javascript/jQuery object (to find what methods and values can be set for example) use the following function:

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}


Or to list them in Firebug:
for(var key in obj){
console.log(key);
}


Solution source: http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object

No comments:

Post a Comment