Results 1 to 4 of 4

Thread: getElementById has no properties

  1. #1
    Join Date
    Sep 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default getElementById has no properties

    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:
    Code:
     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.

    Code:
    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...
    
    	}

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    You can use try { //do stuff here } catch(ex) {}
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    You need to check for the object's existence before checking for existence of any properties.
    Code:
    var o;
    if(o=document.getElementById("foobar") && hasProperties(o)) {
       //do something
       }
    or
    Code:
    var o = document.getElementById("foobar");
    if(o && hasProperties(o)) {
       //do something
       }
    (Different ways of doing the same thing.)
    Trinithis

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Make sure that the code that access element by its ID only after its loading.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •