Returning "Label" value with srcElement

S

stevewy

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
 
N

Neo Geshel

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 <label> element is
supposed to reference the <input> element 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 <label> element’s FOR attribute has a value that is
now identical to the <input> element’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 <input> element whose NAME
attribute has a value of “usernameâ€, and will place the cursor inside
the <input> element’s field automatically. By sharing the same attribute
value, the browser knows that the <label> is linked to the <input>.

Another problem that you have, is that the <label> is not a parent of
the <input> element. As such, you cannot simply reference the parent of
the <input> tag and expect to be able to access the properties of the
<label> element. As such, you will have to walk up the DOM and walk back
down it to the <label> element.

Of course, another really easy way of accessing the <label> element is
to make sure that its FOR attribute is the same as the <input> element’s
NAME and ID attributes (the way it is supposed to be). That way, all you
have to to is get the <input> element’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).
*********************************************************************
 
S

stevewy

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
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top