View Full Version : getElementById has no properties

09-26-2007, 11:00 AM
How do I test if an object has properties? As you see below, I am iterating through a list of ids, in order to get the value of each. However in some instances, not all id elements will be on the page.
What I am looking for is something like 'hasproperties()' so that I could say:

if (hasproperties( document.getElementById('GG_'+id))){
other code...

I am currently getting a js error if the getElementById('GG_'+id) is not on found on the page.

var ids = document.getElementById('ggids').value;
var tmp = ids.split(',');

var id = tmp[i];

var qty = document.getElementById('GG_'+id).value;
other code...


09-26-2007, 11:18 AM
You can use try { //do stuff here } catch(ex) {}

09-26-2007, 04:13 PM
You need to check for the object's existence before checking for existence of any properties.

var o;
if(o=document.getElementById("foobar") && hasProperties(o)) {
//do something


var o = document.getElementById("foobar");
if(o && hasProperties(o)) {
//do something

(Different ways of doing the same thing.)

09-27-2007, 03:16 AM
Make sure that the code that access element by its ID only after its loading.