getElementById: Undefined??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cleary1981
    New Member
    • Jun 2008
    • 178

    getElementById: Undefined??

    Hi,

    I have written a function that creates a new object, the problem I am having is trying to refer to these objects in another function. When using getElemenntById I get a value "undefined" . Does anyone know where im going wrong?

    Code:
    function showObject (){
    	if (request.readyState == 4) {
    	var returned = request.responseText;
    	var splitResult = returned.split(" ");
    	var h = splitResult[0];
    	var w = splitResult[1];	// the dimensions must be set to a scale as they are to big for the screen. 25px represents 100mm
    	h = h/5;
    	w = w/5;
    		
    	cv = document.getElementById("canvas");
    	var newObject = document.createElement('div');
    	newObject.Class = g_objName;
    	newObject.id = "newObject";
    	newObject.innerHTML = g_objName;
    	newObject.alt = g_objName;
    	newObject.style.height = h;
    	newObject.style.width = w;
    	newObject.onmousedown=function(){grab(this);}	
    	cv.appendChild(newObject);
    }
    }
    
    function render () {
    	var ww = document.getElementById("newObject").value;
    	alert(ww);
    	
    
    }
  • rohypnol
    New Member
    • Dec 2007
    • 54

    #2
    I'm not sure about this, but you might want to try to add this (don't replace the old method of setting the id, just add this one right after it):
    Code:
    newObject.setAttribute('id', 'newObject');
    Also, you want to add a line in the showObject() function which sets a variable to true and then write it using an alert at the begining of render() so you're sure that showObejct() is called before render().

    Regards,
    Tom

    Comment

    • cleary1981
      New Member
      • Jun 2008
      • 178

      #3
      i worked it out. I needed the .id instead of .value

      Thanks anyway.

      Comment

      Working...