By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

getElementById: Undefined??

100+
P: 178
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
Share this Question
Share on Google+
2 Replies


rohypnol
P: 54
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

100+
P: 178
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.