Forum Moderators: open

Message Too Old, No Replies

How do I check if an object has properties

         

erikcw

2:50 pm on Mar 23, 2007 (gmt 0)

10+ Year Member



Hi,

I'm working on a script that performs some operations on a page element with an id='example1'.

The problem is, it's an ajax page, and that element is not always there. So if it isn't on the page when the javascript runs, I get an error saying:

$(chkTracker[i][0]) has no properties

How can I check if that object does infact have properties? (something like php's is_set?)

Thanks!
Erik

Fotiman

3:08 pm on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can just include the object to check in the condition of an if statement. If the object is undefined, the condition will evaluate to false. For example:

<script type="text/javascript">
var realObj = {
"foo":"bar"
};
if( realObj )
{
alert( "realObj exists and has foo property: " + realObj["foo"] );
if( realObj["xyz"] )
{
// This will not happen
alert( "realObj has xyz" );
}
else
{
alert( "realObj does not have xyz property" );
}
}
</script>