Hello:
Could someone tell me how I can programatically
(JScript/DHTML) change the value of a Label tag?
For example, this is the HTML code:
<FORM name="myForm">
<LABEL for="myformfield">LabelValue</LABEL>
....
</FORM>
What is the code necessary to change the "LabelValue"
variable?
Thank you for any assistance you can provide.
- Paul
.
Joe Fawcett - 30 Sep 2003 09:59 GMT
> Hello:
>
[quoted text clipped - 15 lines]
> - Paul
> .
Well the easiest way would be to give it an id at creation time <lable
id="lblSurname" for="myformfield"> and then change its innerHTML/innerText
var oLabel = document.getElementById("lblSurname");
oLabel.innerText = "New Value";
//OR
oLabel.innerHTML = "<i>New Value<i>";
if you wish to use formatting.
If you don't have the id, just the identifier of the form field to which it
applies then you need to find it first:
function getLabelFromFormField(FieldId)
{
var oLabel = null;
var sFieldId = FieldId.toLowerCase();
var colLabels = document.getElementsByTagName("label");
for (var i = 0; i < colLabels.length; i++)
{
if (colLabels[i].htmlFor.toLowerCase() == sFieldId)
{
oLabel = colLabels[i];
break;
}
}
return oLabel;
}
I'm not sure if this needs name or id of the form element or will accept
either.
Joe

Signature
Joe