chaning the innertext of a label with javascript

have tried and cannot seem to get this right.


Ta,


Ross
roscoeh23Asked:
Who is Participating?
 
HonorGodConnect With a Mentor Software EngineerCommented:
or to invoke it some time after the page has loaded completely.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Label Modification </title>
<script type='text/javascript'>
  function change( obj ) {
    if ( obj ) {
      obj.value = 'New value';
    }
  }
</script>
</head>
<body>
<input type='button' value='Some value' onclick='change(this)' />
</body>
</html>
0
 
daohailamConnect With a Mentor Commented:
Use innerHTML instead:

document.getElementById('id_of_label').innerHTML = 'some text';
0
 
roscoeh23Author Commented:
my javscript is not great

why does this not work


<script>
document.getElementById('Label').innerHTML = 'some text';
</script>

<label id="Label"></label>
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
daohailamCommented:
You script was activated before the page ready. So, the label with id "Label" may not exist at the time.  Work around solution is move the block '<script>...</script>' after <label>...</label>.
0
 
HonorGodSoftware EngineerCommented:
or is this what you wanted?
--------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Label Modification </title>
<script type='text/javascript'>
  function change( id ) {
    if ( id && document.getElementById ) {
      var item = document.getElementById( id );
      item.innerHTML = 'Modified Label';
    }
  }
</script>
</head>
<body>
<label id='name' for='item'>Some label</label>
<input id='item' type='button' value='Some value' onclick='change("name")' />
</body>
</html>
0
 
HonorGodSoftware EngineerCommented:
Thanks for the 'A'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.