Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill in data in an input field using Javascript directly on page load?

This question has been asked a lot it seems on Stack Overflow but none of the solutions seem to be working. I am developing a web application where I have to fill in data in data fields on page load. Here is my code:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<input type="text" id="datetime" value="" />
<script type="text/javascript">
$(document).on("pageload",function(){
//for fill field
document.getElementById("datetime").value = "here is value";
});
</script>


</body>
</html>

For some reason, when I load the page, no data gets filled in, does anyone see the reason for it?

like image 368
Daniel Avatar asked Dec 28 '25 20:12

Daniel


1 Answers

You don't need jQuery. Try this:

<!DOCTYPE html>
<html>

<body>
  <input type="text" id="datetime" value="" />

  <script>
    window.onload = function() {
      document.getElementById('datetime').value = 'here is value';
    };
  </script>

</body>

</html>
like image 191
timolawl Avatar answered Dec 31 '25 10:12

timolawl