Javascript Form How to - Set input text value








Question

We would like to know how to set input text value.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from  ww w  .  ja  va2  s.c  o m-->
    document.getElementsByName('capacity')[0].value = 'new value from Javascript';
}
</script>
</head>
<body>
  <input name="capacity" type="text" value="blah blah blah">
</body>
</html>

The code above is rendered as follows: