<p>Is there a tag in HTML that will only display its content if JavaScript is enabled? I know <code>&lt;noscript&gt;</code> works the opposite way around, displaying its HTML content when JavaScript is turned off. But I would like to only display a form on a site if JavaScript is available, telling them why they can't use the form if they don't have it.</p>

<p>The only way I know how to do this is with the <code>document.write();</code> method in a script tag, and it seems a bit messy for large amounts of HTML.</p>

[{'answer_id': 30324, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 7, 'selected': True, 'text': '<p>You could have an invisible div that gets shown via JavaScript when the page loads.</p>\n'}, {'answer_id': 30330, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 1, 'selected': False, 'text': "<p>You could also use Javascript to load content from another source file and output that. That may be a bit more black box-is than you're looking for though. </p>\n"}, {'answer_id': 30331, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://weblogs.asp.net/alex_papadimoulis/archive/2008/02/21/scriptonly-the-opposite-of-a-noscript.aspx" rel="nofollow noreferrer">Alex\'s article</a> springs to mind here, however it\'s only applicable if you\'re using ASP.NET - it could be emulated in JavaScript however but again you\'d have to use document.write();</p>\n'}, {'answer_id': 30365, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>You could set the visibility of a paragraph|div to \'hidden\'.</p>\n\n<p>Then in the \'onload\' function, you could set the visibility to \'visible\'.</p>\n\n<p>Something like:</p>\n\n<p>&lt;body onload="javascript:document.getElementById(rec).style.visibility=visible"&gt;\n&lt;p style="visibility: visible" id="rec"&gt;This text to be hidden unless javascript available.&lt;/p&gt;</p>\n'}, {'answer_id': 35318, 'author': 'cpm', 'author_id': 3674, 'author_profile': 'https://Stackoverflow.com/users/3674', 'pm_score': 0, 'selected': False, 'text': '<p>There isn\'t a tag for that. You would need to use javascript to show the text. </p>\n\n<p>Some people already suggested using JS to dynamically set CSS visible. You could also dynamically generate the text with <code>document.getElementById(id).innerHTML = "My Content"</code> or dynamically creating the nodes, but the CSS hack is probably the most straightforward to read.</p>\n'}, {'answer_id': 37782, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 1, 'selected': False, 'text': '<p>Here\'s an example for the hidden div way:</p>\n\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;meta charset="utf-8"&gt;\n        &lt;title&gt;&lt;/title&gt;\n        &lt;style&gt;\n            *[data-when-js-is-on] {\n                display: none;\n            }\n        &lt;/style&gt;\n        &lt;script&gt;\n            document.getElementsByTagName("style")[0].textContent = "";\n        &lt;/script&gt;\n    &lt;/head&gt;\n    &lt;body&gt;\n        &lt;div data-when-js-is-on&gt;\n            JS is on.\n        &lt;/div&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>(You\'d probably have to tweak it for poor IE, but you get the idea.)</p>\n'}, {'answer_id': 431554, 'author': 'Will', 'author_id': 53744, 'author_profile': 'https://Stackoverflow.com/users/53744', 'pm_score': 8, 'selected': False, 'text': '<p>Easiest way I can think of:</p>\n\n<pre><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;noscript&gt;&lt;style&gt; .jsonly { display: none } &lt;/style&gt;&lt;/noscript&gt;\n&lt;/head&gt;\n\n&lt;body&gt;\n    &lt;p class="jsonly"&gt;You are a JavaScript User!&lt;/p&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>No document.write, no scripts, pure CSS.</p>\n'}, {'answer_id': 1229544, 'author': 'alexanderpas', 'author_id': 136173, 'author_profile': 'https://Stackoverflow.com/users/136173', 'pm_score': 3, 'selected': False, 'text': '<p>First of all, always separate content, markup and behaviour!</p>\n\n<p>Now, if you\'re using the jQuery library (you really should, it makes JavaScript a lot easier), the following code should do:</p>\n\n<pre><code>$(document).ready(function() {\n    $("body").addClass("js");\n});\n</code></pre>\n\n<p>This will give you an additional class on the body when JS is enabled.\nNow, in CSS, you can hide the area when the JS class is not available, and show the area when JS is available.</p>\n\n<p>Alternatively, you can add <code>no-js</code> as the the default class to your body tag, and use this code:</p>\n\n<pre><code>$(document).ready(function() {\n    $("body").removeClass("no-js");\n    $("body").addClass("js");\n});\n</code></pre>\n\n<p>Remember that it is still displayed if CSS is disabled.</p>\n'}, {'answer_id': 3556881, 'author': 'Chris Bloom', 'author_id': 83743, 'author_profile': 'https://Stackoverflow.com/users/83743', 'pm_score': 3, 'selected': False, 'text': '<p>I don\'t really agree with all the answers here about embedding the HTML beforehand and hiding it with CSS until it is again shown with JS. Even w/o JavaScript enabled, that node still exists in the DOM. True, most browsers (even accessibility browsers) will ignore it, but it still exists and there may be odd times when that comes back to bite you.</p>\n\n<p>My preferred method would be to use jQuery to generate the content. If it will be a lot of content, then you can save it as an HTML fragment (just the HTML you will want to show and none of the html, body, head, etc. tags) then use jQuery\'s ajax functions to load it into the full page.</p>\n\n<h2>test.html</h2>\n\n<pre><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;\n    &lt;script type="text/javascript" charset="utf-8"&gt;\n     $(document).ready(function() {\n       $.get(\'_test.html\', function(html) {\n         $(\'p:first\').after(html);\n       });\n     });\n    &lt;/script&gt;\n&lt;/head&gt;\n&lt;body&gt;\n  &lt;p&gt;This is content at the top of the page.&lt;/p&gt;\n  &lt;p&gt;This is content at the bottom of the page.&lt;/p&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<h2>_test.html</h2>\n\n<pre><code>&lt;p&gt;This is from an HTML fragment document&lt;/p&gt;\n</code></pre>\n\n<h2>result</h2>\n\n<pre><code>&lt;p&gt;This is content at the top of the page.&lt;/p&gt;\n&lt;p&gt;This is from an HTML fragment document&lt;/p&gt;\n&lt;p&gt;This is content at the bottom of the page.&lt;/p&gt;\n</code></pre>\n'}, {'answer_id': 6226046, 'author': 'Spycho', 'author_id': 660311, 'author_profile': 'https://Stackoverflow.com/users/660311', 'pm_score': 2, 'selected': False, 'text': '<p>I have a simple and flexible solution, somewhat similar to Will\'s (but with the added benefit of being valid html):</p>\n\n<p>Give the body element a class of "jsOff". Remove (or replace) this with JavaScript. Have CSS to hide any elements with a class of "jsOnly" with a parent element with a class of "jsOff".</p>\n\n<p>This means that if JavaScript is enabled, the "jsOff" class will be removed from the body. This will mean that elements with a class of "jsOnly" will not have a parent with a class of "jsOff" and so will not match the CSS selector that hides them, thus they will be shown.</p>\n\n<p>If JavaScript is disabled, the "jsOff" class <strong>will not</strong> be removed from the body. Elements with "jsOnly" <strong>will</strong> have a parent with "jsOff" and so <strong>will</strong> match the CSS selector that hides them, thus they will be hidden.</p>\n\n<p>Here\'s the code:</p>\n\n<pre><code>&lt;html&gt;\n    &lt;head&gt;\n        &lt;!-- put this in a separate stylesheet --&gt;\n        &lt;style type="text/css"&gt;\n            .jsOff .jsOnly{\n                display:none;\n            }\n        &lt;/style&gt;\n    &lt;/head&gt;\n\n    &lt;body class="jsOff"&gt;\n        &lt;script type="text/javascript"&gt;\n            document.body.className = document.body.className.replace(\'jsOff\',\'jsOn\');\n        &lt;/script&gt;\n\n        &lt;noscript&gt;&lt;p&gt;Please enable JavaScript and then refresh the page.&lt;/p&gt;&lt;/noscript&gt;\n\n        &lt;p class="jsOnly"&gt;I am only shown if JS is enabled&lt;/p&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>It\'s valid html. It is simple. It\'s flexible.</p>\n\n<p>Just add the "jsOnly" class to any element that you want to only display when JS is enabled.</p>\n\n<p>Please note that the JavaScript that removes the "jsOff" class should be executed as early as possible inside the body tag. It cannot be executed earlier, as the body tag will not be there yet. It should not be executed later as it will mean that elements with the "jsOnly" class may not be visible right away (as they will match the CSS selector that hides them until the "jsOff" class is removed from the body element).</p>\n\n<p>This could also provide a mechanism for js-only styling (e.g. <code>.jsOn .someClass{}</code>) and no-js-only styling (e.g. <code>.jsOff .someOtherClass{}</code>). You could use it to provide an alternative to <code>&lt;noscript&gt;</code>:</p>\n\n<pre><code>.jsOn .noJsOnly{\n    display:none;\n}\n</code></pre>\n'}, {'answer_id': 17724149, 'author': 'prgDevelop', 'author_id': 527962, 'author_profile': 'https://Stackoverflow.com/users/527962', 'pm_score': 1, 'selected': False, 'text': '<p>My solution</p>\n\n<p><strong>.css:</strong></p>\n\n<pre><code>.js {\ndisplay: none;\n}\n</code></pre>\n\n<p><strong>.js:</strong></p>\n\n<pre><code>$(document).ready(function() {\n    $(".js").css(\'display\', \'inline\');\n    $(".no-js").css(\'display\', \'none\');\n});\n</code></pre>\n\n<p><strong>.html:</strong></p>\n\n<pre><code>&lt;span class="js"&gt;Javascript is enabled&lt;/span&gt;\n&lt;span class="no-js"&gt;Javascript is disabled&lt;/span&gt;\n</code></pre>\n'}, {'answer_id': 57666343, 'author': 'Chuck', 'author_id': 8661144, 'author_profile': 'https://Stackoverflow.com/users/8661144', 'pm_score': 2, 'selected': False, 'text': '<p>In the decade since this question was asked, the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden" rel="nofollow noreferrer"><code>HIDDEN</code> attribute</a> was added to HTML. It allows one to directly hide elements without using CSS. As with CSS-based solutions, the element must be un-hidden by script:</p>\n\n<pre><code>&lt;form hidden id=f&gt;\nJavascript is on, form is visible.&lt;br&gt;\n&lt;button&gt;Click Me&lt;/button&gt;\n&lt;/form&gt;\n&lt;script&gt;\ndocument.getElementById(\'f\').hidden=false;\n&lt;/script&gt;\n&lt;noscript&gt;\nJavascript is off, but form is hidden, even when CSS is disabled.\n&lt;/noscript&gt;\n\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30319', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2098/']