By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Returning "Label" value with srcElement

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thanks - that's what I was looking for!

Steve

Feb 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.