473,403 Members | 2,359 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,403 software developers and data experts.

Input type label's value

how to set input type label's value through javascript

Mar 29 '07 #1
4 24429
On Mar 29, 1:08 am, "pradeep" <pwprad...@gmail.comwrote:
how to set input type label's value through javascript
assuming "in" is input element and "newVal" is new value for label.

var labels=document.getElementsByTagName("label"),i;
for( i=0; i<labels.length;i++ )
if(labels[i].htmlFor==in.id)labels[i].innerHTML=newVal
--
will be easy if you just set an id on label and the use getElementById

Mar 29 '07 #2
On Mar 29, 5:23 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Mar 29, 1:08 am, "pradeep" <pwprad...@gmail.comwrote:
how to set input type label's value through javascript

assuming "in" is input element and "newVal" is new value for label.
Javascript has an "in" operator, so not a good choice for a local
variable.

The W3C HTML specification doesn't define a value attribute for label
elements, so assigning a value attribute to a label and giving it a
value may be unreliable for whatever (undisclosed) purpose the OP has
in mind.
>
var labels=document.getElementsByTagName("label"),i;
for( i=0; i<labels.length;i++ )
if(labels[i].htmlFor==in.id)labels[i].innerHTML=newVal
The for attribute is not mandatory, the label can be related to an
input by being its parent:

<label><input ... ></label>

is quite valid, though IE doesn't correctly associate the label with
the input so not recommended.
will be easy if you just set an id on label and the use getElementById
I guess you're suggesting something like a scheme that assigns a label
id which is the same as the input id or name with a prefix of "label",
e.g.:

<label id="label_i01"><input name="i01"></label>

Then the label id can be used to find the input id or name and hence
get a reference to the appropriate element. However, also be aware
that a label can be associated with more than one control, any
solution should accommodate that.
--
Rob

Mar 29 '07 #3
On Mar 29, 3:29 am, "RobG" <r...@iinet.net.auwrote:
var labels=document.getElementsByTagName("label"),i;
for( i=0; i<labels.length;i++ )
if(labels[i].htmlFor==in.id)labels[i].innerHTML=newVal

The for attribute is not mandatory, the label can be related to an
input by being its parent:
so this should work-
function getLabel(iElem){
if(/label/i.test(iElem.parentElement.tagName))
return iElem.parentElement
var labels=document.getElementsByTagName("label"),i;
for( i=0; i<labels.length;i++ ){
if(labels[i].htmlFor==iElem.id)
return labels[i]
}
return false
}

will be easy if you just set an id on label and the use getElementById

I guess you're suggesting something like a scheme that assigns a label
id which is the same as the input id or name with a prefix of "label",
e.g.:

<label id="label_i01"><input name="i01"></label>

Then the label id can be used to find the input id or name and hence
get a reference to the appropriate element.
yes.
However, also be aware
that a label can be associated with more than one control, any
solution should accommodate that.
How ? like this :

<label >
Name : <input type="text">
Pass : <input type="password">
</label>

but it will work for first control only, right ?

Mar 29 '07 #4
On Mar 29, 9:41 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Mar 29, 3:29 am, "RobG" <r...@iinet.net.auwrote:
[...]
However, also be aware
that a label can be associated with more than one control, any
solution should accommodate that.

How ? like this :

<label >
Name : <input type="text">
Pass : <input type="password">
</label>
Yes.
>
but it will work for first control only, right ?
Clicking on the second control should put focus on the first. I
should also have said that the for attribute associates a label with
exactly one control, but that a control can have more than one label -
I assume so you can layout forms without splitting labels over block
elements, e.g. (wrapped for posting):

<form action="">
<table>
<tr>
<td style="text-align:right;"><label
for="foo">Ipsum</label></td>
<td><label for="foo">Lorem</label></td>
<td><input type="text" id="foo" name="foo"></td>
</tr>
<tr>
<td style="text-align:right;"><label
for="bar">sequestor</label></td>
<td><label for="bar">ce</label></td>
<td><input type="text" id="bar" name="bar"></td>
</tr>
<tr>
<td colspan="3"><label>Multi<input name="b1">
<input name="b2"></label></td>
</tr>
</table>
</form>
Probably a bad example, but hopefully it does the job - it "works" in
Firefox, can't say what IE will make of it. Probably best to follow-
up in an HTML group. :-)
--
Rob

Mar 29 '07 #5

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

Similar topics

6
by: Boyd Reilly | last post by:
I have a form that has the user pick the type of question he will answer. The input field will be a text, numeric or date type. So, after the question is answered, I need to change the input...
15
by: Mattia | last post by:
Hi! I have a <form> that can be submitted thruogh three buttons and I need to tell witch one was pressed when the form was submitted. I now do it with 3 <input type="submit" name="..."...
3
by: leeaids | last post by:
Hi, I am hoping somebody can help me. Normally to use the file upload feature in an html page, users must either select or type in a file to upload then click on button or link to initiate...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to set up a 'control panel' consisting of a table of icons. The early stages: http://www.deepinit.com/controlcentre.php Each of these is set up like: <td> <input...
1
by: spolsky | last post by:
try the the following code with Opera 9.01 (Windows). when clicked slightly faster than normal clicking, the toggler checkbox and other checkboxes displays differently although event method works...
0
by: Rotsey | last post by:
Hi, I have a application that fills a html hidden control with xml serverside. I use javascript to read the value of the control on the client. The problem is the value property is blank...
4
by: stevewy | last post by:
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...
4
by: ckirchho | last post by:
Hallo, In a form I have several text or checkbox inputs with their appropriate labels. I'd like to keep label and input together, so I connect them with a &nbsp;, like: <input type="checkbox"...
1
by: mishrarajesh44 | last post by:
hi all, i am writting a program to update the record in the mysql databqse, using php. I hav designed the 'update form' totally same as my 'add form' so that by entering the mobile no. of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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,...

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.