473,569 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

label for object

We have a massive java application to be made ADA compliant. We want onfocus
and onblur events for each text field. The best way seems to be javascript,
by cycling through all the nodes recursively after page load, and attaching
the events. We already cycle through everything to set tabs. If nodename is
LABEL, we should be able to use the FOR attribute, get the object
referenced, and attach events on that object.

When cycling through nodes, if I get to a label I can print the attribute
collection, and there is a FOR having the correct value for the associated
text object. I can't get any further than that.

This is a piece of code:

if (child.nodeName =='LABEL') {
//var sss = ''+child.attrib utes.length+"\n ";
//for (var k=0;k<child.att ributes.length; k++) sss +=
''+child.attrib utes[k].nodeName+'='+c hild.attributes[k].nodeValue+"\n" ;
//alert(sss);
try {
var cont = child.attribute s.getNamedItem( "for");
alert(''+cont.n odeName);
cont.attacheven t('onfocus',ale rt("xxx"));
cont.attacheven t('onblur',aler t("zzz"));
}
catch (e) {
}
}

Maybe by reading the code you can see what I'm trying to do. When it
executes "alert(''+cont. nodeName);", it displays "for". I need to display
"textarea" because that's what the label is associated with. If anyone can
help I'd appreciate it.

Jul 23 '05 #1
8 4438
VK
cont.nodeValue will give you the object ID (but *not* the reference).
So you need to:

var cont = child.attribute s.getNamedItem( *"for");
var obj = document.getEle mentById(cont.n odeValue);
// attachEvent, not attachevent - case makes difference:
obj.attachEvent ('onfocus',ale* rt("xxx"));
obj.attachEvent ('onblur',aler* t("zzz"));

I predict a big mess though because label and textbox are sharing the
same event stream (thus clicking on label will trig 'onclick' for
associated textbox either).
So it's going to be a bunch of dublicate events you'll have to kill
with returnValue=nul l;

Jul 23 '05 #2
VK
> kill with returnValue=nul l;

returnValue=fal se;

or by checking event.srcElemen t.tagName ("LABEL" or "INPUT") every time.

Jul 23 '05 #3
Yep
David McDivitt wrote:

Hi,
We have a massive java application to be made ADA compliant. We want onfocus
and onblur events for each text field. The best way seems to be javascript,
by cycling through all the nodes recursively after page load, and attaching
the events. We already cycle through everything to set tabs. If nodename is
LABEL, we should be able to use the FOR attribute, get the object
referenced, and attach events on that object.

<style type="text/css">
label input[type=text] {background-color:#ccc;}
</style>

<form>
<label for="foo1"><inp ut type="text" id="foo1"></label>
<label for="foo2"><inp ut type="text" id="foo2"></label>
<label for="foo3"><inp ut type="text" id="foo3"></label>
</form>

<script type="text/javascript">
var Utils = {

//getElementsWith TagName
getElementsWith TagName : function(root, tag) {
var d=document, func, fail=null;
if(d.all) {
func = function(root, tag) {
return root && tag &&
(tag=="*" ? root.all : root.all.tags(t ag)) || fail;
}
} else if(d.getElement sByTagName) {
func = function(root, tag) {
return root && tag && root.getElement sByTagName(tag) || fail;
}
} else {
func = function(){retu rn fail;}
}
return (Utils.getEleme ntsWithTagName = func)(root, tag);
},

//getElementWithI d
getElementWithI d : function(id) {
var d=document, func, fail=null;
if(d.getElement ById) {
func = function(id) {return d.getElementByI d(id);}
} else if(d.all) {
func = function(id) {return d.all[id]; }
} else {
func = function() {return fail;}
}
return (Utils.getEleme ntWithId = func)(id);
},

//addEventListene r
addEventListene r : function(obj, evt, handler) {
var d=document, func;
if(d.addEventLi stener) {
func = function(obj, evt, handler) {
evt=evt.replace (/^on/i,"").toLowerCa se();
obj.addEventLis tener(evt, handler, false);
}
} else {
func = function(obj, evt, handler) {
evt="on"+evt.re place(/^on/i,"").toLowerCa se();
obj[evt]=handler;
}
}
return (Utils.addEvent Listener = func)(obj, evt, handler);
}
};

function initLabels() {
var labels=Utils.ge tElementsWithTa gName(document. body, "label");
if(labels){
for(var ii=labels.lengt h; ii--;) {
var target=Utils.ge tElementWithId( labels[ii].htmlFor);
if(target && target.type.toL owerCase()=="te xt") {
Utils.addEventL istener(target, "onfocus", focusHandler);
Utils.addEventL istener(target, "onblur", blurHandler);
}
}
}
function focusHandler(){ this.style.back groundColor="ye llow";}
function blurHandler(){t his.style.backg roundColor="";}
}

Utils.addEventL istener(window, "onload", initLabels);
</script>
HTH,
Yep.

Jul 23 '05 #4
YD
David McDivitt a écrit :
We have a massive java application to be made ADA compliant. We want onfocus
and onblur events for each text field. The best way seems to be javascript,
by cycling through all the nodes recursively after page load, and attaching
the events. We already cycle through everything to set tabs. If nodename is
LABEL, we should be able to use the FOR attribute, get the object
referenced, and attach events on that object.
If you cycle through everything, why don't you attach events on the node named
'TEXTAREA'?
var cont = child.attribute s.getNamedItem( "for");


then cont is the attribute 'FOR' of the element you call child.
To achieve what you want, try:

var cont=document.g etElementById(c hild.attributes .getNamedItem(" for").nodeValue );

as the value of the FOR element is the id of the form element it refers.

HTH

--
Y.D.
Jul 23 '05 #5
>From: "VK" <sc**********@y ahoo.com>
Subject: Re: label for object
Date: 8 Jun 2005 13:45:44 -0700

cont.nodeVal ue will give you the object ID (but *not* the reference).
So you need to:

var cont = child.attribute s.getNamedItem( *"for");
var obj = document.getEle mentById(cont.n odeValue);
// attachEvent, not attachevent - case makes difference:
obj.attachEven t('onfocus',ale *rt("xxx"));
obj.attachEven t('onblur',aler *t("zzz"));

I predict a big mess though because label and textbox are sharing the
same event stream (thus clicking on label will trig 'onclick' for
associated textbox either).
So it's going to be a bunch of dublicate events you'll have to kill
with returnValue=nul l;


Thanks VK. YD helped, too.

What I have now is:

if (child.nodeName =='LABEL') {
try {
var cont = child.attribute s.getNamedItem( "for");
var obj = document.getEle mentById(cont.n odeValue);
alert(''+obj.no deName); //says INPUT or TEXTAREA, good
obj.attachEvent ("onfocus",aler t("xxx"));
obj.attachEvent ("onblur",alert ("zzz"));
}
catch (e) {
}
}

What should be the onfocus event executes while the javascript is running,
so I get several xxx alert boxes. Clicking on the textarea does not generate
an onfocus. In reading documentation for attachEvent, I don't know if syntax
allows arguments.

When finished it will be something like:

if (child.nodeName =='LABEL') {
try {
var cont = child.attribute s.getNamedItem( "for");
var obj = document.getEle mentById(cont.n odeValue);
obj.attachEvent ("onfocus",wind ow.status=<labe l text>);
obj.attachEvent ("onblur",windo w.status='');
}
catch (e) {
}
}

Doing this will save endless man hours. All we have to do is put label tags
on all the pages. We are using the struts framework. The id of text, input,
textarea, etc., is set with the styleId attribute in the <HTML:WHATEVE R
struts tag, and that matches FOR in the label tag.

Jul 23 '05 #6
David McDivitt wrote:

Hi,
obj.attachEvent ("onfocus",aler t("xxx"));


obj.attachEvent ("onfocus", functionRef);

The second argument should be a function reference, like

function funcRef(){windo w.status=""}
FYI, your using try/catch and attachEvent limits your application to IE
on intranets, which may or not be fine to you.
HTH,
Yep.
Jul 23 '05 #7
VK
attachEvent() supports anonymous function call:
obj.attachEvent ('onfocus', function(args){ body;});

*But* it will lead to the circular reference memory leak in IE:
<http://support.microso ft.com/default.aspx?sc id=kb;en-us;830555>
(say thank you to Martin Honner, Matt Kruz and Co who pulled out this
not so well known bug).

On your scale (> "We have a massive java application") this leak may
eventually crash the system.

The only way to secure your memory on IE is to use pure function
references. Also if you guarantee that you need one function call per
event and this call is static (will not be altered/canceled later), you
are free to use universal property set instead of attachEvent:
obj.onfocus = f1;
obj.onblur = f2;

The trick is to store the LABEL object reference in the INPUT object.
This solves all your problem:

<html>
<head>
<title>Untitl ed Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
<!-- BHW: ADA also requires to not lock users to a partucular platform
and software.
So could we be a bit more cross-browser friendly? ;-) Like this
code. -->
function init() {
var arr = document.getEle mentsByTagName( 'LABEL');
var len = arr.length;
var txt = null;
for (i=0; i<len; i++) {
txt = document.getEle mentById((arr[i].htmlFor));
txt.thisLabel = arr[i];
txt.onfocus = f1;
txt.onblur = f2;
}
}
function f1(e) {
var obj = (e)? e.target.thisLa bel : event.srcElemen t.thisLabel;
obj.style.color ='red';
}
function f2(e) {
var obj = (e)? e.target.thisLa bel : event.srcElemen t.thisLabel;
obj.style.color ='black';
}
window.onload = init;
</script>
</head>

<body bgcolor="#FFFFF F">
<form>
<label id="oLbl" for="oTxt1" accesskey="1">L abel <u>1</u>:</label>
<input type="text" id="oTxt1">
<label id="oLb2" for="oTxt2" accesskey="2">L abel <u>2</u>:</label>
<input type="text" id="oTxt2">
</form>
</body>
</html>

The posted code is the most resource/time wise. But if you need to
iterate through the DOM nodes anyway for other reasons, then just keep
your approach.

Jul 23 '05 #8
>From: "VK" <sc**********@y ahoo.com>
Subject: Re: label for object
Date: 9 Jun 2005 02:14:05 -0700

attachEvent( ) supports anonymous function call:
obj.attachEven t('onfocus', function(args){ body;});

*But* it will lead to the circular reference memory leak in IE:
<http://support.microso ft.com/default.aspx?sc id=kb;en-us;830555>
(say thank you to Martin Honner, Matt Kruz and Co who pulled out this
not so well known bug).

On your scale (> "We have a massive java application") this leak may
eventually crash the system.

The only way to secure your memory on IE is to use pure function
references. Also if you guarantee that you need one function call per
event and this call is static (will not be altered/canceled later), you
are free to use universal property set instead of attachEvent:
obj.onfocus = f1;
obj.onblur = f2;

The trick is to store the LABEL object reference in the INPUT object.
This solves all your problem:

<html>
<head>
<title>Untitle d Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
<!-- BHW: ADA also requires to not lock users to a partucular platform
and software.
So could we be a bit more cross-browser friendly? ;-) Like this
code. -->
function init() {
var arr = document.getEle mentsByTagName( 'LABEL');
var len = arr.length;
var txt = null;
for (i=0; i<len; i++) {
txt = document.getEle mentById((arr[i].htmlFor));
txt.thisLabel = arr[i];
txt.onfocus = f1;
txt.onblur = f2;
}
}
function f1(e) {
var obj = (e)? e.target.thisLa bel : event.srcElemen t.thisLabel;
obj.style.color ='red';
}
function f2(e) {
var obj = (e)? e.target.thisLa bel : event.srcElemen t.thisLabel;
obj.style.color ='black';
}
window.onloa d = init;
</script>
</head>

<body bgcolor="#FFFFF F">
<form>
<label id="oLbl" for="oTxt1" accesskey="1">L abel <u>1</u>:</label>
<input type="text" id="oTxt1">
<label id="oLb2" for="oTxt2" accesskey="2">L abel <u>2</u>:</label>
<input type="text" id="oTxt2">
</form>
</body>
</html>

The posted code is the most resource/time wise. But if you need to
iterate through the DOM nodes anyway for other reasons, then just keep
your approach.


Thanks VK! You made my day. I adapted my code and it works perfect.

Jul 23 '05 #9

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

Similar topics

4
12307
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text (centered in the label) on this label. My font on this label is Courier new - hence fixed width character cells.
1
9182
by: nospamjac | last post by:
Hi, Is there a way to update the text of an asp:label on a webform without refreshing the entire page? What is called by button clicks and other events that refresh a webform control? See the example WebForm1.aspx and WebForm1.aspx.cs code below: WebForm1.aspx =======================================================
31
7169
by: jcrouse | last post by:
Is there a quick and easy way to change the color of a label controls border from the default black to white? Thank you, John
6
3138
by: jcrouse | last post by:
I am rotating some text is some label controls. In the one place I use it it works fine. In the other place I use it I can't figure out the syntax. I don't really understand the event. Where it works fine, it seems to fire when the form changes visibility. Here is the code. Private Sub lblP1JoyUp_Paint(ByVal sender As Object, ByVal e As...
3
1840
by: Dustan | last post by:
I have a Button object that gets replaced by a Label when clicked. Button(buttonsframe,text=' ',command=c,font=buttonsFont) Note that the text is a single space. buttonsFont uses 'Courier New' as a family. When clicked, the Button is destroyed and replaced with a Label object: Label(buttonsframe,text=x,font=buttonsFont,relief=RAISED) ...
12
13163
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the same Tag number. (I thought it might help) The checkboxes name is a concatenation of "chkCancel" and a number that represents the order in which...
1
2873
by: cnixuser | last post by:
Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text file which includes some html code to populate html formatted content as the text of an asp:label (<asp:label>). The reading of the text file itself...
2
2340
by: Andy B | last post by:
I have the following listView control on a page: <asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1"> <ItemTemplate> <dl> <dt>
9
7995
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have a MDI Container Form, with one label control on the Background of this container form. When I now open a child form, this form is behind the label ... and this looks ugly ... :-)) When I set the label with "SendToBack", it completely disapears. I also tried to set the child form like "BringToFront" but this also does not...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.