:: Forum >>

When should use id?

I am wondering when I can use id instead of variable. For example,

var control=new AW.UI.Input;
control.setId("id");
document.write(control);//works
document.write(id);//not work
document.write(document.getElementById(id));//not work

document.write(control) works, but document.write(id) or document.write(document.getElementById(id)) don't work.

Actually, I need to dynamically create some controls and set them a meaningly name as refrences. For example,

var c=new Array();
for (var i=0; i<100; i++){c[i]=new AW.UI.Input;}
c[0].setId("Apple");
c[1].setId("Bear");
c[2].setId("Chart");
......
Apple.setControlText("red");
Bear.setControlText("black");
Chart.setControlText("Color");

Could you give me an idea when should I use varable and when should I use ID?

Thanks,
Sunday, April 27, 2008
Setting an id sets the idname of the resulting HTML object. That is -
<div id=someidname class=someclassname
That is why your document.write(id) doesn't work. document.write() expects an object which has a toString() method which will generate the underlying HTML code for the object.

The best way to observe this is to use FireFox with the FireBug debugger plugin and examine the generated HTML.

In your specific code example, c[0].setId("Apple") does not assign a variable name to that particular array element, so you can't later do Apple.setControlText("red"). Instead, you would need to continue using the variable c[0] as an array element (unless you assign it to another variable with the name Apple. That is -
var c = []
var apple
...
apple = c[0]
Anthony
Sunday, April 27, 2008

This topic is archived.


Back to support forum

Forum search