Client-Side Object Reference Quandry

F

Fred

Please consider the following two if() structures. The first one fails, yet
the second works. Why? FWIW, I got the syntax of the first out of a working
page. I figured moving it to another page would be harmless. What's going on
here? The only major difference between the two pages that I can see so far
is that in the new page (where the first if() structure fails), the control
imgname is in a <form runat="server" method="post"> whereas in the page it
came from, there is no <Form> at all on the page. Could that have some
relevance?

// This one fails
if(v_incoming != "")
{
imgname.value = v_incoming; // this errors out with message: 'imgname' is
undefined.
}

// This one works
if(v_incoming != "")
{
var obj_imgname = document.all("imgname");
obj_imgname.value = v_incoming; //no problems
}

here is the definition of imgname:
<input type="text" id="imgname" size="40" value="http://" NAME="imgname">

Thanks!
 
B

bruce barker

yes, having a form defined matters.

<html>
<body>
<input type=text name=imgname id=imgname1>
<form name=myForm>
<input type=text name=imgname id=imgname1>
</form>
</body>
</html>

document.imgname.value //refers to the first one
document.myForm.imgname.value //refers to the second (as it is a child
of myForm)

document.all("imgname") // is obsolete and
should never be used - would return an array

document.getElementsByTag('imgname')[0] // referes to the first one
document.getElementsByTag('imgname')[1] // referes to the second one

as id are required to be unique in a document unlike names, the following
should work

document.getElementById('imgname1') // referes to the first one
document.getElementById('imgname2') // referes to the second one

-- bruce (sqlwork.com)
 
F

Fred

Wow, thanks. I guess I'll have to take JavaScript a bit more seriously!


bruce barker said:
yes, having a form defined matters.

<html>
<body>
<input type=text name=imgname id=imgname1>
<form name=myForm>
<input type=text name=imgname id=imgname1>
</form>
</body>
</html>

document.imgname.value //refers to the first one
document.myForm.imgname.value //refers to the second (as it is a child
of myForm)

document.all("imgname") // is obsolete and
should never be used - would return an array

document.getElementsByTag('imgname')[0] // referes to the first one
document.getElementsByTag('imgname')[1] // referes to the second one

as id are required to be unique in a document unlike names, the following
should work

document.getElementById('imgname1') // referes to the first one
document.getElementById('imgname2') // referes to the second one

-- bruce (sqlwork.com)


Fred said:
Please consider the following two if() structures. The first one fails, yet
the second works. Why? FWIW, I got the syntax of the first out of a working
page. I figured moving it to another page would be harmless. What's
going
on
here? The only major difference between the two pages that I can see so far
is that in the new page (where the first if() structure fails), the control
imgname is in a <form runat="server" method="post"> whereas in the page it
came from, there is no <Form> at all on the page. Could that have some
relevance?

// This one fails
if(v_incoming != "")
{
imgname.value = v_incoming; // this errors out with message: 'imgname' is
undefined.
}

// This one works
if(v_incoming != "")
{
var obj_imgname = document.all("imgname");
obj_imgname.value = v_incoming; //no problems
}

here is the definition of imgname:
<input type="text" id="imgname" size="40" value="http://" NAME="imgname">

Thanks!
 
B

Bobby Ryzhy

In your first example - imagename is undefined. An easier way to do this is to use your form name.

example:

<form name="Form1">
<input type="text" id="imgname" size="40" value="http://" NAME="imgname">
</form>

if(v_incoming != "")
{
Form1.imgname.value = v_incoming;
}

Bobby Ryzhy
bobby@ domain below
http://weekendtech.net

Please consider the following two if() structures. The first one fails, yet
the second works. Why? FWIW, I got the syntax of the first out of a working
page. I figured moving it to another page would be harmless. What's going on
here? The only major difference between the two pages that I can see so far
is that in the new page (where the first if() structure fails), the control
imgname is in a <form runat="server" method="post"> whereas in the page it
came from, there is no <Form> at all on the page. Could that have some
relevance?

// This one fails
if(v_incoming != "")
{
imgname.value = v_incoming; // this errors out with message: 'imgname' is
undefined.
}

// This one works
if(v_incoming != "")
{
var obj_imgname = document.all("imgname");
obj_imgname.value = v_incoming; //no problems
}

here is the definition of imgname:
<input type="text" id="imgname" size="40" value="http://" NAME="imgname">

Thanks!

Bobby Ryzhy
bobby @ domain below
http://weekendtech.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top