user544079
user544079

Reputation: 16639

clear input textbox value on load

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>

Upvotes: 6

Views: 41304

Answers (5)

Thielicious
Thielicious

Reputation: 4452

onload=()=>{
    document.getElementById('user').value='';
}

or

onload=()=>{
    document.getElementById('user').setAttribute('value','');
}

Upvotes: 0

PherricOxide
PherricOxide

Reputation: 15929

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) 

Upvotes: 9

Chase
Chase

Reputation: 29569

Try something like:

window.onload = function(){
 document.getElementById("user").value = "";
}

Here's an example

Upvotes: 2

Lzh
Lzh

Reputation: 3635

Maybe you can add a script tag just after the form.

<script type="text/javascript">document.getElementById("user").value = "";</script>

You can also use jQuery to subscribe to both an individual element's ready (or load) event or the document.

Upvotes: 0

AMember
AMember

Reputation: 3057

You can run a timeout that will clear the field values. like this:

var t=setTimeout(function(){CLEARVALUES},3000)

Upvotes: 0

Related Questions