3

I have a simple HTML page where I am trying to populate a label with a JavaScript variable

<html>
<body onload="loading();">
      <div>
          <h4>Main Page</h4>
          <label id="cookie1" style="color: #0026ff"/> 
<!--MORE CODE-->
      </div>
</body>
</html>

My Javascript function is shown below. It will not populate the label on the html page but if I change the label to a textfield it will work (but i want a label). I have also used 'innerhtml' which does populate the label but it ONLY shows this label. I have more fields and buttons that are erased or hidden if i use 'innerhtml'. Is there something I am missing?

function loading() {
    var val1 = "Site:   " + getCookie("storeSite");
    document.getElementById("cookie1").value = val1;
}

2 Answers 2

8

You are setting the value. Try to set the innerHTML.

document.getElementById("cookie1").innerHTML = val1;

A similar JSFiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

To clarify: value is a commonly-used property in <input> tags. But, most other element types just have an innerHTML property, as well as innerText/textContent depending on the browser.
ya, i did use innerhtml but it still wouldnt work. My issue was that i was going <label /> instead of <label></label>. But thanks!
0

This line in your function

document.getElementById("cookie1").value = val1;

Should have been

 document.getElementById("cookie1").innerHTML = val1;

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.