-1

Lets say I have

<html>
<head>
<script>
function myFunction()
{
// blah blah
}
</script>
</head>
<body>
<select name="mySelect">
<option value="1"> He </option>
<option value="2"> Hi </option>
<option value="3"> Ho </option>
</select>
</body>
</html>

Lets say I have js function that generates this 'select' script. How can I add to it default script that will be ran as soon as DOM is loaded with jQuery?

something like that:

<select name="mySelect" script="myFunction()">
<option value="1"> He </option>
<option value="2"> Hi </option>
<option value="3"> Ho </option>
</select>
Townsheriff
  • 569
  • 6
  • 14

5 Answers5

1
$(window).load(function() {
    // your code here
});

Useful stuff here

Community
  • 1
  • 1
martynas
  • 12,120
  • 3
  • 55
  • 60
1

Well if your are avoiding jQuery for this then:

window.onload = myFunction;

with jQuery:

$(document).ready(myFunction);

or

$(window).load(myFunction);
Jai
  • 74,255
  • 12
  • 74
  • 103
0
$(document).ready(function() {
    // after DOM is ready
});
$(window).load(function() {
    // after DOM including graphics ready
   // specially used for images' script
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • Better way to check document ready event is : `jQuery(document).ready(function($) { // to load dom });` – ahgindia Jun 18 '14 at 11:58
0

You should use window.onload

like

<script>window.onload = myFunction</script>
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

You can also use document ready

 $(document).ready(function(){
// do here

});

$( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).load(function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

Balachandran
  • 9,567
  • 1
  • 16
  • 26