473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why are labels handled differently than an input control with clie

Here is the rendered output from my page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>Untitled Page</title></head>
<body>
<form name="aspnetForm" method="post" action="child.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKMTAzNzI3NDIwMGRkZWiDaQtY8r7RUqQI1w/SRH9RC8k=" />
</div>
<div>
<div>
<label id="ctl00_ph_lbl">label</label>
<input name="ctl00$ph$txt" type="text" id="ctl00_ph_txt"
value="textbox" />
<button id="ctl00_ph_btn" onclick="test()" value="abcdefg">
</button>
</div>

<script type="text/javascript" language="javascript">
function test(){
alert(ctl00_ph_lbl.innerText);
alert(document.aspnetForm.ctl00_ph_txt.value);
}
</script>
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION"
id="__EVENTVALIDATION"
value="/wEWAgLYyMeTBwLaocbcB+Tmua4QHW6PavIEFZo2N7QLNNk9" />
</div>
</form>
</body>
</html>

In the javascript, how come I have to add "document.aspnetForm" before the
clientId code on the input control but not on the label control? If I omit
"document.aspnetForm" from the alert line for the input control, I get the
error message "ctl00_ph_txt is undefined". Is there some definition of when
a control needs this and when it doesn't?
Aug 1 '06 #1
2 1403
I'm not 100% sure but I think only <input /controls are added to the
form's control collection (on the client, that is), so the label is
located in the document and the textbox in the form.
You are usually better of getting a reference to the control with
javascript by using the getElementByID() function.
Then you can just do getElementByID('ctl00_ph_lbl').innerText and
getElementByID('ctl00_ph_txt').value.

Consistent and nice =)

//Mats

ccgatvolvo wrote:
Here is the rendered output from my page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>Untitled Page</title></head>
<body>
<form name="aspnetForm" method="post" action="child.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKMTAzNzI3NDIwMGRkZWiDaQtY8r7RUqQI1w/SRH9RC8k=" />
</div>
<div>
<div>
<label id="ctl00_ph_lbl">label</label>
<input name="ctl00$ph$txt" type="text" id="ctl00_ph_txt"
value="textbox" />
<button id="ctl00_ph_btn" onclick="test()" value="abcdefg">
</button>
</div>

<script type="text/javascript" language="javascript">
function test(){
alert(ctl00_ph_lbl.innerText);
alert(document.aspnetForm.ctl00_ph_txt.value);
}
</script>
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION"
id="__EVENTVALIDATION"
value="/wEWAgLYyMeTBwLaocbcB+Tmua4QHW6PavIEFZo2N7QLNNk9" />
</div>
</form>
</body>
</html>

In the javascript, how come I have to add "document.aspnetForm" before the
clientId code on the input control but not on the label control? If I omit
"document.aspnetForm" from the alert line for the input control, I get the
error message "ctl00_ph_txt is undefined". Is there some definition of when
a control needs this and when it doesn't?
Aug 1 '06 #2
That's incorrect. Both the controls are in the forms Controls collection.

I don't want to hard code the clientId ("ctl00_ph_txt") into the javascript
because if the control was moved into another container object, the id
would then be wrong. However, in this case, using
getElementByID('ctl00_ph_txt')
as a test does not solve the problem, it still can't get a reference to the
control.

"Ma***@newsgroups.nospam" wrote:
I'm not 100% sure but I think only <input /controls are added to the
form's control collection (on the client, that is), so the label is
located in the document and the textbox in the form.
You are usually better of getting a reference to the control with
javascript by using the getElementByID() function.
Then you can just do getElementByID('ctl00_ph_lbl').innerText and
getElementByID('ctl00_ph_txt').value.

Consistent and nice =)

//Mats

ccgatvolvo wrote:
Here is the rendered output from my page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>Untitled Page</title></head>
<body>
<form name="aspnetForm" method="post" action="child.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKMTAzNzI3NDIwMGRkZWiDaQtY8r7RUqQI1w/SRH9RC8k=" />
</div>
<div>
<div>
<label id="ctl00_ph_lbl">label</label>
<input name="ctl00$ph$txt" type="text" id="ctl00_ph_txt"
value="textbox" />
<button id="ctl00_ph_btn" onclick="test()" value="abcdefg">
</button>
</div>

<script type="text/javascript" language="javascript">
function test(){
alert(ctl00_ph_lbl.innerText);
alert(document.aspnetForm.ctl00_ph_txt.value);
}
</script>
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION"
id="__EVENTVALIDATION"
value="/wEWAgLYyMeTBwLaocbcB+Tmua4QHW6PavIEFZo2N7QLNNk9" />
</div>
</form>
</body>
</html>

In the javascript, how come I have to add "document.aspnetForm" before the
clientId code on the input control but not on the label control? If I omit
"document.aspnetForm" from the alert line for the input control, I get the
error message "ctl00_ph_txt is undefined". Is there some definition of when
a control needs this and when it doesn't?
Aug 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin O'Rourke | last post by:
All, I am hoping someone might be able to put me out of my misery and let me know if it is possible or not to dervie the name of an element in a form, based on its associated label, only knowing...
2
by: DBQueen | last post by:
I have a database which will be printing out labels for SMALL test tubes (1/4" high). We have yet to find a reasonably-priced printer (labelwriter) which can effectively print this on ROLLS of...
1
by: Kyle Blaney | last post by:
When labels are vertically stacked on top of one another and have their TextAlign property set to MiddleRight, the text is not properly right-aligned. You can reproduce this problem by creating...
2
by: Mike Borrowdale | last post by:
-- Hi, my application simulates a panel of scientific instruments. Digital readouts are provided by label components embedded in a TableLayoutPanel with the rows and columns set to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.