2

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!

2
  • Please more info, value of which element? Cuz it is useless to set value of label Commented Mar 31, 2013 at 18:08
  • I meant set the value of the innerHTML, I have now learned. Commented Mar 31, 2013 at 18:22

3 Answers 3

3

jsFiddle Demo

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";
 }
}
Sign up to request clarification or add additional context in comments.

Comments

1
<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

Comments

0

A label has no value. If you want to set the text, you may use

element.innerHTML = "some text";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.