469,271 Members | 1,356 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

getElementById: Undefined??

178 100+
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?

Expand|Select|Wrap|Line Numbers
  1. function showObject (){
  2.     if (request.readyState == 4) {
  3.     var returned = request.responseText;
  4.     var splitResult = returned.split(" ");
  5.     var h = splitResult[0];
  6.     var w = splitResult[1];    // the dimensions must be set to a scale as they are to big for the screen. 25px represents 100mm
  7.     h = h/5;
  8.     w = w/5;
  9.  
  10.     cv = document.getElementById("canvas");
  11.     var newObject = document.createElement('div');
  12.     newObject.Class = g_objName;
  13.     newObject.id = "newObject";
  14.     newObject.innerHTML = g_objName;
  15.     newObject.alt = g_objName;
  16.     newObject.style.height = h;
  17.     newObject.style.width = w;
  18.     newObject.onmousedown=function(){grab(this);}    
  19.     cv.appendChild(newObject);
  20. }
  21. }
  22.  
  23. function render () {
  24.     var ww = document.getElementById("newObject").value;
  25.     alert(ww);
  26.  
  27.  
  28. }
Jul 28 '08 #1
2 3237
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):
Expand|Select|Wrap|Line Numbers
  1. 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
Jul 28 '08 #2
cleary1981
178 100+
i worked it out. I needed the .id instead of .value

Thanks anyway.
Jul 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by dlolsen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.