clear input textbox value on load

dom, javascript

Solution

You can use window.onload instead.

function init() {
    // Clear forms here
    document.getElementById("user").value = "";
}
window.onload = init;

You could also use one of the nice wrappers for doing this in JQuery, Dojo, or you favorite Javascript library. In JQuery it would be,

$(document).ready(init) 

Problem

I have the following form which is getting pre populated with values on load. I want to have empty input boxes on load using Javascript. However I do not have access to the `<body>` tag so `<body onload=""` will not work here. Is there any other way to clear the form fields on load? ``` <form method="POST" action=""> <input name="username" id="user" value="" /><br /> <input name="pwd" id="pass" value="" /> <input type="submit" value="Go" /> </form> ```

Original source