Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear input textbox value on load

Tags:

javascript

dom

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>
like image 364
user544079 Avatar asked Sep 06 '26 07:09

user544079


2 Answers

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) 
like image 68
PherricOxide Avatar answered Sep 07 '26 20:09

PherricOxide


Try something like:

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

Here's an example

like image 26
Chase Avatar answered Sep 07 '26 21:09

Chase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!