Reputation: 431
I would like to set a value of a <label>
, like so:
<label for="idname">Value here...</label>
with Javascript. I have already done this, for the for
attribute:
element.setAttribute("for", "idname");
is there something like element.setValue()
that I can use to set the value of the label? Thanks!
Upvotes: 2
Views: 10305
Reputation: 82277
Iterate through the label elements looking for the property for="idname"
like this:
var labels = document.getElementsByTagName("label");
for( var i = 0; i < labels.length; i++ ){
if( labels[i].outerHTML.indexOf('for="idname"') > -1){
var UseLabelValue = labels[i].innerHTML;
labels[i].innerHTML = "Replace Value";
}
}
Upvotes: 3
Reputation: 11177
<label for="idname">Value here...</label>
<script>
document.getElementsByTagName('label')[0].innerHTML='new value';
</script>
https://developer.mozilla.org/ru/docs/DOM/element.innerHTML
http://javascript.info/tutorial/searching-elements-dom
Upvotes: 1
Reputation: 382132
A label has no value. If you want to set the text, you may use
element.innerHTML = "some text";
Upvotes: 0