I am trying to understand $(document).ready with this, perhaps, silly JSFIDDLE example (this basically uses jQuery to draw 10 divs with a title Graph1...) There is numerous question on this already here and here. But what i want is to understand the flow/order of how this works, relative to the DOM..etc. Why I should use it and why I should not. I am trying to use this silly example to understand it better
Specifically, with the jsfiddle example, Why does it not work with noWrap - in <body> and $(document).ready(function(){ is commented out?
Note: Maybe this example is too silly or unclear to wha I want, but with your comments I will hopefully be able to clarify it and get a better understanding.
<!DOCTYPE html> <html> <head> <title>Access Links</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- <script src="../src/jquery-1.11.0.min.js"></script> <!-- works here with $(document).ready(function(){ commented out --> <!-- DOES NOT WORK here with $(document).ready(function(){ --> </head> <body> <!-- <script src="../src/jquery-1.11.0.min.js"></script> <!-- works here with $(document).ready(function(){ commented out --> <!-- DOES NOT WORK here with $(document).ready(function(){ --> <div class="container" id="graphs"> <!-- My divs will go here --> </div> <!-- <script src="../src/jquery-1.11.0.min.js"></script> <!-- works here with $(document).ready(function(){ commented out --> <!-- DOES NOT WORK here with $(document).ready(function(){ --> <script> //$(document).ready(function(){ //--------- START: Draw all your divs------------------------------------------> var numberOfDivs =10; for (var x = 1; x <= numberOfDivs; x += 1) { var $div = $('<div class="chart half"><h3 id="g' + x + '">Graph' + x + '</h3><svg id="chart' + x + '"></svg></div>'); $('#graphs').append($div); } //--------- END: Draw all your divs------------------------------------------> //} </script> <!-- <script src="../src/jquery-1.11.0.min.js"></script> <!-- DOES NOT work here with $(document).ready(function(){ commented out --> <!-- DOES NOT WORK here with $(document).ready(function(){ --> </body> </html>
$(document).ready()since the elements are referenced after they're created in the DOM.$(document).ready()? Well, you'd use it when you don't want your code to execute until the document'sreadyevent, which is invoked when the DOM is complete.