-11

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>

6
  • 2
    Do you need it... for what? Have you inspected the result of stack snippets? I’m not very clear on what exactly you are asking.
    – yivi
    Commented Jul 14, 2018 at 16:38
  • 3
    Why not try that out? You get to see a preview of your post as you edit it.
    – Martijn Pieters Mod
    Commented Jul 14, 2018 at 16:42
  • 2
    From the duplicate: HTML - The top-left pane. Only the body content goes here. When you run the snippet, it will be wrapped in markup including <body>...</body> tags.. That post is linked from the help center page on formatting.
    – Martijn Pieters Mod
    Commented Jul 14, 2018 at 16:44
  • 1
    A follow-up question should be made in an actual follow-up question. Make a new question, ask your follow-up, and link the original question that it's a follow-up to. The way that you've done this is a chameleon question, and is very much the wrong way to go about asking, either here on meta, but especially on the main SO site. Commented Jul 14, 2018 at 17:15
  • I've rolled back the edit adding a new question. Stack Overflow aims to build a collection of questions and answers for future visitors to find and get a benefit from. They need to be able to identify the question as covering the same problem they are seeing, and putting multiple questions into one post muddies the waters. Moreover, it makes it harder to determine if the answers given address just the initial question, the additional question, or both. Don't add questions to existing posts, please.
    – Martijn Pieters Mod
    Commented Jul 14, 2018 at 18:03
  • Related: meta.stackoverflow.com/q/272915/4356188
    – clickbait
    Commented Jul 15, 2018 at 4:13

1 Answer 1

3

There's no need for <html>, <head>, etc. tags in a code snippet. It will take care of the heavy lifting for you, allowing you to concentrate on the important parts of the question.

Think as it of a built-in way to help you produce an MCVE.

6
  • What if I need to use the head?
    – clickbait
    Commented Jul 14, 2018 at 16:52
  • @sag and why you will need the head? there is no need for title, meta, etc, etc .. and if you want to inlcude external CSS or JS you can do it within the body Commented Jul 14, 2018 at 20:33
  • @TemaniAfif What if the question asks about the usage of a meta tag? Can the answerer make examples with snippets?
    – clickbait
    Commented Jul 14, 2018 at 20:45
  • @sag and what will be the purpose of a runnable snippet if it's about meta tag? ... you don't need. The main purpose of snippet code is to create a running code. Commented Jul 14, 2018 at 20:58
  • @TemaniAfif Like if someone wants to demo mobile device scaling or character encodings.
    – clickbait
    Commented Jul 14, 2018 at 21:03
  • Related: meta.stackoverflow.com/q/272915/4356188
    – clickbait
    Commented Jul 15, 2018 at 4:14

Not the answer you're looking for? Browse other questions tagged .