How to call jQuery function in HTML <body> onload?
6 answers
This section has been reviewed here earlier.
You are most likely looking
$(document).ready(function() {
var input_id;
//code
})
or
$(window).load(function($) {
var input_id;
//code
});
If you are interested in learning about the differences between the two, see the jQuery documentation on this topic.
+3
JQuery 3, :
document.ready(function(){
//code
});
The recommended alternative in JQuery 3 is to use the following syntax (which in previous versions was considered a shorthand syntax):
$(function(){
//code
});
Here is the official Jquery explanation of why the first syntax was discounted and no longer recommended ( https://api.jquery.com/ready/ ):
... The selection of [document] does not affect the behavior of the .ready () method, which is inefficient and can lead to incorrect assumptions about the behavior of the method.
0