RELATED: Allow us to choose a doctype for Stack Snippets
I can put this in a code snippet:
$("#box").css("color", "red");
<div id="box">Hello, World!</div>
I can also do this:
$(document).ready(() => {
$("#box").css("color", "red");
});
<!doctype html>
<html>
<head></head>
<body>
<div id="box">Hello, World!</div>
</body>
</html>
Does the code snippet system automatically fill in the <html>
, <head>
, <body>
, etc. and start executing JS when the page has finished loading, or are you supposed to put that manually?
Follow-Up Question
According to the answer, code snippets take care of the heavy lifting. What if you want to put stuff in the <head>? Will the wrapper elements be duplicated, or will the snippets accommodate for it?
For instance, someone who wants to show differences between character encodings has to use a <meta> tag in the <head>
<body>...</body>
tags.. That post is linked from the help center page on formatting.