Why is $(document).ready(function() { }); necessary for #8?


#1

I understood the syntax correctly to modify the height/width/css properties for the div and was typing it correctly, but the code wouldn't pass unless it was nested in a document.ready function. The Hint on this makes it seem like it's not necessary, but I'm just confused if it's required at all times?


#2

Hi R3vertion,

Surrounding all of your code in $(document).ready(...); isn't always necessary, but it's usually a good practice - if you try to select an element that doesn't exist yet because the browser processed your JavaScript code before the HTML, it will throw an error and do nothing. By using $(document).ready(...);, we can ensure that everything we would be trying to modify will already be loaded, so there won't be any errors from that :slight_smile: