Set the value of an input field Set the value of an input field javascript javascript

Set the value of an input field


This is one way of doing it:

document.getElementById("mytext").value = "My value";


I use 'setAttribute' function:

<input type="text" id="example"> // Setup text field <script type="text/javascript">   document.getElementById("example").setAttribute('value','My default value');</script>


if your form contains an input field like

<input type='text' id='id1' />

then you can write the code in javascript as given below to set its value as

document.getElementById('id1').value='text to be displayed' ;