Web Analytics Made Easy -
StatCounter getElementById returning null w/ dynamic object? - CodingForum

Announcement

Collapse
No announcement yet.

getElementById returning null w/ dynamic object?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getElementById returning null w/ dynamic object?

    Hey all,

    Sorry, I've tried, but am really stumped here...

    The error: "document.getElementById(options[i]) is null"

    The code:
    Code:
    var options = new Array("blank", "cncOnly", "updosOnly", "mensOnly", "cncUpdos", "cncMens", "updoMens", "allThree", "makeoverCuts", "makeoverHaircolor");
    function testToggleMenu(myDiv) {
    /*
    	var optionsString = "";
     	for (var j = 0; j < options.length; j++) {
    		optionsString += options[j] + ", ";
    	}
    */
    //	alert ("passed div id = " + myDiv + " /// options = " + optionsString + " //");
    //	alert("options.length = " + options.length);
    
    	for(var i = 0; i < options.length; i++) {
    		var currentOption = options[i];
    		if (options[i] == myDiv) {
    //			alert ("Option matched using: " + options[i] + " !");
    			document.getElementById(options[i]).style.display = "block";
    		} else {
    //			alert ("Option not matched using: " + options[i] + " !");
    			document.getElementById(options[i]).style.display = "none";
    		}
    	}
    }
    You can see the debugging alerts I tried to implement - and it passes all of those measures, it just doesn't seem to want to use the value of options[i] as the id - is there some voodoo going on with getElementById and dynamic objects that I'm anaware of... or... ?

    Any help would be greatly appreciated,
    Thanks,
    TE

  • #2
    Works fine, but it sounds like you're missing an element with one of the ids listed in the array (keep in mind that they _are_ case-sensitive). Check your HTML... or post it... and we'll look into it. The reason it breaks is that it can't get one of the elements.... a simple alert in the for loop should point you in the right direction.... here:

    Code:
    	for(var i = 0; i < options.length; i++) {
    		alert(options[i]);
    		// rest of code
    	}
    If you can't solve it after that... just post the HTML and I... or someone else will look into it.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

    Comment


    • #3
      Though I guess another way to do this would be to:

      Code:
      	for(var i = 0; i < options.length; i++) {
      		var currentOption = options[i];
      		[COLOR="Red"]if (document.getElementById(currentOption) != null)[/COLOR] {
      			if (options[i] == myDiv) {
      //				alert ("Option matched using: " + options[i] + " !");
      				document.getElementById(options[i]).style.display = "block";
      			} else {
      //				alert ("Option not matched using: " + options[i] + " !");
      				document.getElementById(options[i]).style.display = "none";
      			}
      		}
      	}
      But eh... whichever floats your boat.
      The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

      How to ask smart questions?

      Comment


      • #4
        Holy smack! *sigh* Thanks!

        I thought, "of course they're all there, and I know they're case-sensative"...
        But I used this to test each div independently:
        Code:
        	if (document.getElementById('makeoverHaircolor')) {
        		alert("Found makeoverHaircolor");
        	} else {
        		alert("COULDN'T FIND makeoverHaircolor");
        	}
        yeah - well, I had commented out one of the html blocks while I was testing another section.

        Got it now, working beautifully.
        Thanks,
        TE

        Comment

        Working...
        X