Javascript DOM How to - Convert all text in specific element to uppercase








Question

We would like to know how to convert all text in specific element to uppercase.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--  w w w .  j  a va 2s  .c o m-->
    var headers = document.getElementsByTagName('th');
    for( var i = 0, len = headers.length; i < len; i++ ){
        headers[i].innerHTML = headers[i].innerHTML.toUpperCase();
    }
}
</script>
</head>
<body>
  <table>
    <tr>
      <th>product name</th>
      <th>product id</th>
    </tr>
  </table>
</body>
</html>

The code above is rendered as follows: