471,887 Members | 828 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

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 3300
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
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.