all groups > dotnet jscript > march 2005 >
You're in the

dotnet jscript

group:

Can't find element in javascript



Can't find element in javascript clsmith66
3/22/2005 10:53:03 AM
dotnet jscript: Hopefully someone can help me because I am very confused. I need to write a
javascript function to set the properties on an activeX control I wrote. For
some reason though any javascript I write in my ASP.NET page can't fnd even
the simplest control. Just for test I have the following code:
function Test() {
alert(document.Form1.hdnCustName.value);
}
Every time I run this function it throws an error saying
document.Form1.hdnCustName.value is null or not an object. I've double
checked the control name and even set the value of the control in the html.
Nothing. I get a similar error if I use document.getElementById. Can anyone
help me find what I might be missing? I've used similar and more complex
code in the past and cannot figure out why this piece will not work.

Thanks in advance
Re: Can't find element in javascript Bruce Barker
3/22/2005 3:22:50 PM
active/x controls are not form elements, and thus are not form children, so
you cannot reference them from the form as you are trying.

try:

myControl = document.getElementById('hdnCustName'); // supply id attribute
on <object tag>
myControl = document.getElementByName('hdnCustName')[0]; // supply name
attribute on <object tag>


also active/x control are loaded async. they can be null until the onload
event fires. properties should not be accessed until readyState == 4, as the
object maynot be created or init until then.

so you normally do something like:

// loop until ready - burns cpu - should use setTimeout
myControl = null;
while (true)
{
myControl = document.getElementById('hdnCustName');
if (myControl && myControl .readyState == 4)
break
}

// can now access control (may never get here)

myControl.prop = ...


-- bruce (sqlwork.com)



[quoted text, click to view]

Re: Can't find element in javascript Serge Baltic
3/23/2005 4:38:16 AM
bb> // loop until ready - burns cpu - should use setTimeout
bb> myControl = null;
bb> while (true)

Not should but must; either setTimeout or setINter
(H) Serge

AddThis Social Bookmark Button