473,387 Members | 1,456 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,387 software developers and data experts.

Returning "Label" value with srcElement

If I am using srcElement (or "target" for non-IE models) to return
various properties of an object I have clicked on, can I access for
"label for" value in any way? I'm thinking, for example, of an input
text box like this:

<label for="fred">Username:</label>
<input type="text" name="hello" id="hello1" size="10">

Now, once a function has:

var obj=event.srcElement || event.target;

.... at the top, I should be able to access obj.name and get "hello"
etc, but how can I access the label value of "fred"? I did try
obj.label but it didn't work...

Steve Wylie
Canterbury
England

Feb 5 '07 #1
4 1990
st*****@hotmail.com wrote:
If I am using srcElement (or "target" for non-IE models) to return
various properties of an object I have clicked on, can I access for
"label for" value in any way? I'm thinking, for example, of an input
text box like this:

<label for="fred">Username:</label>
<input type="text" name="hello" id="hello1" size="10">

Now, once a function has:

var obj=event.srcElement || event.target;

... at the top, I should be able to access obj.name and get "hello"
etc, but how can I access the label value of "fred"? I did try
obj.label but it didn't work...

Steve Wylie
Canterbury
England
One of the problems you have right away, is that the <labelelement is
supposed to reference the <inputelement properly, and your example is
not doing so.

This would be a proper setup:

<label for="username">Username:</label>
<input type="text" name="username" id="username" size="10">

As you can see, the <labelelement’s FOR attribute has a value that is
now identical to the <inputelement’s NAME attribute. That way, when
someone clicks on the “Username:” text, their browser knows that they
want to edit or add to the contents of the <inputelement whose NAME
attribute has a value of “username”, and will place the cursor inside
the <inputelement’s field automatically. By sharing the same attribute
value, the browser knows that the <labelis linked to the <input>.

Another problem that you have, is that the <labelis not a parent of
the <inputelement. As such, you cannot simply reference the parent of
the <inputtag and expect to be able to access the properties of the
<labelelement. As such, you will have to walk up the DOM and walk back
down it to the <labelelement.

Of course, another really easy way of accessing the <labelelement is
to make sure that its FOR attribute is the same as the <inputelement’s
NAME and ID attributes (the way it is supposed to be). That way, all you
have to to is get the <inputelement’s ID, and you then have the
<label>’s FOR attribute value to filter for.

Without seeing more of your JS, this is the best example I can provide.

I hope this helps.
...Geshel
--
************************************************** *******************
My return e-mail address is an automatically monitored spam honeypot.
Do not send e-mail there unless you wish to be reported as a spammer.
Please send all e-mail to my first name at my last name dot org, with
a subject-line of “NEWSGROUP REPLY FOR NEO GESHEL” (all uppercase).
************************************************** *******************
Feb 5 '07 #2
st*****@hotmail.com wrote:
<label for="fred">Username:</label>
how can I access the label value of "fred"?
If you have the label element object then you can read out the 'htmlFor'
property
element.htmlFor
see
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-96509813>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 5 '07 #3
Apologies - the script I provided was by way of an example (bad one,
obviously) and is not the actual code I am using. I hammered it out
in a hurry without realising that the Label needs to be the same as
the element or it doesn't make sense! Thanks anyway.

Steve

Feb 6 '07 #4
Thanks - that's what I was looking for!

Steve

Feb 6 '07 #5

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

Similar topics

0
by: Paul | last post by:
I am using Visual Studio.net 2005 (and asp.net 2.0) Let's say that in a Web Project, I have created a (Dataset?) called SomeDataset.xsd. On it I have a TableAdapter called SomeTableTableAdapter...
6
by: Flavio | last post by:
Hi I am havin a problem with urllib2.urlopen. I get this error when I try to pass a unicode to it. raise UnicodeError, "label too long" is this problem avoidable? no browser or programs such...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
1
by: alien_attack | last post by:
I have the a repeater which is databound to an XmlDatasource. Inside the repeater is a Label as follows: <asp:Label ID="lbl" runat="server" Text='<%# XPath("Asset_Metadata/@Value")%>' /> ...
5
jayam
by: jayam | last post by:
Hai everybody, I want to know the reason why the text box which has the label "Name" appears yellow in color. I want to know is there any special reason behind it. bye jayam
4
RMWChaos
by: RMWChaos | last post by:
Darnit all, I expect the code I steal from others to work! =D Below is some code that I got to initiate multiple javascripts on page load (rather than using the "onload=" attribute). According the...
0
by: navneet gupta | last post by:
I want to put labels on a specified position.How can i do this.
2
by: Jethro | last post by:
Hi guys, I'm a bit of a newbie, but know VB Classic quite well. The above statement appears in some code I'm hacking, and I have added a column to the DataGrid (in the middle, so those to the...
3
by: Akino877 | last post by:
Hello, I am trying to user a File Chooser with DIRECTORIES_ONLY set. And I would like to be able to hide the "Files of type" label and the associated combo box. I wonder if there is a way to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.