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