PDA

View Full Version : getElementById has no properties



irish
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(',');

for(i=0;i<tmp.length;i++){
var id = tmp[i];

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

}

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

Trinithis
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
}

or


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

(Different ways of doing the same thing.)

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