Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to change text within a label tag (or equivalent)

Status
Not open for further replies.

wrbodine

Programmer
Aug 24, 2000
302
US
Hi,

I have labels for text boxes on an html form, and I need to change the text that's displayed between these labels when a certain drop-down value is selected. I know how to call a JavaScript client-side function in the onChange attribute of the select box, but how do I change the text in the label? Or is there another way to accomplish this?

like, right now I've got
<label for=&quot;IDNumber&quot;>Driver's License Number</label>
<input type=&quot;text&quot; name=&quot;IDNumber&quot; value=&quot;strIDNumber&quot;>

how can I modify the words &quot;Driver's License Number&quot;?

Thanks,
Ray
 
Does it have to be label? Some tags support the .innerHTML property. You just refer to object.innerHTML, setting it equal to a text string.

Thomas D. Greer
 
label tags are fine. Use innerHTML if you want HTML, or you can use the simpler innerText property:

Code:
<html>
<body>
<label id=&quot;IDNumber&quot;>Driver's License Number</label>
<br>
<button onClick=&quot;x=document.getElementById('IDNumber'); x.innerText='New Label text';&quot;>Change Label</button>
</body>
</html>

Thomas D. Greer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top