Daniel
Daniel

Reputation: 269

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?

Upvotes: 3

Views: 5307

Answers (3)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

In Jquery it's called document ready event... So use the below syntax.

$(document).ready(function(){
 //for fill field
 $("#datetime").val( "here is value");
});

Note the inner line of code has been changed.. This is the Jquery way of doing the same thing... You can also use this Shorthand to $(document).ready(function(){ That is $(function(){

Upvotes: 0

karina
karina

Reputation: 805

What version of jquery are you using? Looks like pageload might deprecated and you should use pagecontainerload

Or better yet use the $(document).ready or .load methods?

Upvotes: 0

timolawl
timolawl

Reputation: 5564

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>

Upvotes: 5

Related Questions