CINXE.COM

InnerHTML In JavaScript

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content="innerhtml in javascript, HTML, getElementById, dom, inner html, javascript, how to create"> <meta name="Description" content="Examples of innerHTML within HTML documents. Includes copy/paste codes."> <link rel="canonical" href="https://www.quackit.com/javascript/tutorial/innerhtml_in_javascript.cfm"> <title>InnerHTML In JavaScript</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <link rel="shortcut icon" href="/pix/favicon96.png"> <link rel="apple-touch-icon" href="/pix/apple-touch-icon.png"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet"> <link href="/common/css/master.45.min.css" rel="stylesheet"> <script async src="https://cdn.fuseplatform.net/publift/tags/2/3499/fuse.js"></script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-Q3H025ZKLN"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q3H025ZKLN'); </script> </head> <body> <header class="site-header"> <div class="site-header-base"> <div class="site-logo"> <a title="Quackit Homepage" target="_top" href="/"><img src="/pix/quackit_logo_watermark.png" width="87" height="33" alt="Quackit Logo"></a> </div> <button id="site-nav-toggler" class="site-nav-toggler" aria-expanded="false" aria-controls="site-nav"> <span class="sr-only">Toggle navigation</span> &#9776; </button> </div> <nav id="site-nav" class="site-nav"> <div class="site-links"> <ul> <li><a href="/"><i class="fa fa-home"></i> <span class="sr-only">Home</span></a></li> <li><a href="/html/">HTML</a></li> <li><a href="/css/">CSS</a></li> <li><a href="/scripting/">Scripting</a></li> <li><a href="/database/">Database</a></li> </ul> </div> <div class="site-search-top"> <form action="/search/" id="cse-search-box-bottom" class="site-search"> <div> <input type="hidden" name="cx" value="partner-pub-6331358926293806:98x0fk-bbgi"> <input type="hidden" name="cof" value="FORID:10"> <input type="hidden" name="ie" value="ISO-8859-1"> <input type="text" name="q" size="20" class="site-search-input"> <button type="submit" name="sa" class="site-search-button"><i class="fa fa-search"></i></button> </div> </form> </div> </nav> </header> <div class="main"> <article class="content"> <h1 class="page-title">InnerHTML In JavaScript</h1> <div class="ad ad-top"> <!-- GAM 71161633/QCKIT_quackit/article_header --> <div data-fuse="23059883623"></div> </div> <ul class="pager"> <li><a href="/javascript/tutorial/two_dimensional_arrays.cfm"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> 2D Arrays</a></li> <li><a href="/javascript/tutorial/unobtrusive_javascript.cfm">Unobtrusive JavaScript <i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></li> </ul> <p class="lead">The <code>innerHTML</code> property can be used to modify your document's HTML on the fly.</p> <p>When you use <code>innerHTML</code>, you can change the page's content without refreshing the page. This can make your website feel quicker and more responsive to user input.</p> <p>The <code>innerHTML</code> property can be used along with <code>getElementById()</code> within your JavaScript code to refer to an HTML element and change its contents.</p> <h2>The <code>innerHTML</code> Syntax</h2> <p>You can use <code>innerHTML</code> to set content like this:</p> <script src="/common/js/codemirror/lib/codemirror.js"></script> <script src="/common/js/codemirror/mode/javascript/javascript.js"></script> <div class="code-only"> <textarea id="example1" autocomplete="off" spellcheck="false">document.getElementById&#x28;&#x27;&#x7b;ID of element&#x7d;&#x27;&#x29;.innerHTML &#x3d; &#x27;&#x7b;content&#x7d;&#x27;&#x3b;</textarea> </div> <script> var exampleCode1 = CodeMirror.fromTextArea(document.getElementById("example1"), { mode: "text/javascript", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <p>In this syntax example, <code>{ID of element}</code> is the ID of an HTML element and <code>{content}</code> is the new content to go into the element.</p> <h2>Basic <code>innerHTML</code> Example</h2> <p>Here's a basic example to demonstrate how <code>innerHTML</code> works. </p> <p>This code includes two functions and two buttons. Each function displays a different message and each button triggers a different function. </p> <p>In the functions, the <code>getElementById()</code> refers to the HTML element by using its ID. We give the HTML element an ID of <samp>myText</samp> using <code>id="myText"</code>. </p> <p>So in the first function for example, you can see that <code>document.getElementById('myText').innerHTML = 'Hey &lt;strong&gt;Thanks!&lt;/strong&gt;';</code> is setting the inner HTML of the <samp>myText</samp> element to <samp>Hey Thanks!</samp>.</p> <script src="/common/js/codemirror/mode/htmlmixed/htmlmixed.js"></script> <script src="/common/js/codemirror/mode/css/css.js"></script> <script src="/common/js/codemirror/mode/xml/xml.js"></script> <div class="code-only"> <textarea id="example2" autocomplete="off" spellcheck="false">&lt;script&gt;&#xd;&#xa;function Msg1&#x28;&#x29;&#x7b;&#xd;&#xa; document.getElementById&#x28;&#x27;myText&#x27;&#x29;.innerHTML &#x3d; &#x27;Hey &lt;strong&gt;Thanks&#x21;&lt;&#x2f;strong&gt;&#x27;&#x3b;&#xd;&#xa;&#x7d;&#xd;&#xa;function Msg2&#x28;&#x29;&#x7b;&#xd;&#xa; document.getElementById&#x28;&#x27;myText&#x27;&#x29;.innerHTML &#x3d; &#x27;Try &lt;strong&gt;message 1&lt;&#x2f;strong&gt; again...&#x27;&#x3b;&#xd;&#xa;&#x7d;&#xd;&#xa;&lt;&#x2f;script&gt;&#xd;&#xa;&lt;input type&#x3d;&quot;button&quot; onclick&#x3d;&quot;Msg1&#x28;&#x29;&quot; value&#x3d;&quot;Show Message 1&quot;&gt;&#xd;&#xa;&lt;input type&#x3d;&quot;button&quot; onclick&#x3d;&quot;Msg2&#x28;&#x29;&quot; value&#x3d;&quot;Show Message 2&quot;&gt;&#xd;&#xa;&lt;p id&#x3d;&quot;myText&quot;&gt;&lt;&#x2f;p&gt;</textarea> </div> <script> var exampleCode2 = CodeMirror.fromTextArea(document.getElementById("example2"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <h2>Example of <code>innerHTML</code> With User Input</h2> <p>Here's an example of using <code>innerHTML</code> with a text field. Here, we display whatever the user has entered into the input field.</p> <div class="code-only"> <textarea id="example3" autocomplete="off" spellcheck="false">&lt;script&gt;&#xd;&#xa;function showMsg&#x28;&#x29;&#x7b;&#xd;&#xa; var userInput &#x3d; document.getElementById&#x28;&#x27;userInput&#x27;&#x29;.value&#x3b;&#xd;&#xa; document.getElementById&#x28;&#x27;userMsg&#x27;&#x29;.innerHTML &#x3d; userInput&#x3b;&#xd;&#xa;&#x7d;&#xd;&#xa;&lt;&#x2f;script&gt;&#xd;&#xa;&lt;input type&#x3d;&quot;input&quot; maxlength&#x3d;&quot;40&quot; id&#x3d;&quot;userInput&quot; onkeyup&#x3d;&quot;showMsg&#x28;&#x29;&quot; placeholder&#x3d;&quot;Enter text here...&quot;&gt;&#xd;&#xa;&lt;p id&#x3d;&quot;userMsg&quot;&gt;&lt;&#x2f;p&gt;</textarea> </div> <script> var exampleCode3 = CodeMirror.fromTextArea(document.getElementById("example3"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <h2>User Input with <code>textContent</code></h2> <p>In a case like the previous example, we probably don't want the user to enter any HTML. We just want them to enter their name in plain text. When outputting plain text without HTML tags, it's usually more appropriate to use <code>textContent</code> rather than <code>innerHTML</code>.</p> <p>So if we replace <code>innerHTML</code> with <code>textContent</code> it would look like this: </p> <div class="code-only"> <textarea id="example4" autocomplete="off" spellcheck="false">&lt;script&gt;&#xd;&#xa;function showMsg2&#x28;&#x29;&#x7b;&#xd;&#xa; var userInput &#x3d; document.getElementById&#x28;&#x27;userInput2&#x27;&#x29;.value&#x3b;&#xd;&#xa; document.getElementById&#x28;&#x27;userMsg2&#x27;&#x29;.textContent &#x3d; userInput&#x3b;&#xd;&#xa;&#x7d;&#xd;&#xa;&lt;&#x2f;script&gt;&#xd;&#xa;&lt;input type&#x3d;&quot;input&quot; maxlength&#x3d;&quot;40&quot; id&#x3d;&quot;userInput2&quot; onkeyup&#x3d;&quot;showMsg2&#x28;&#x29;&quot; placeholder&#x3d;&quot;Enter text here...&quot;&gt;&#xd;&#xa;&lt;p id&#x3d;&quot;userMsg2&quot;&gt;&lt;&#x2f;p&gt;</textarea> </div> <script> var exampleCode4 = CodeMirror.fromTextArea(document.getElementById("example4"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <h2>Formatting with <code>getElementById()</code></h2> <p><code>innerHTML</code> and <code>textContent</code> are just two of many properties we can use to manipulate the DOM. </p> <p>In this example, we use <code>getElementById()</code> to to detect the color that the user has selected. We then use it again, in conjunction with <code>style.background</code> to apply the new color. In both cases, we refer to the HTML elements by their ID (using <code>getElementById()</code>.)</p> <div class="code-only"> <textarea id="example5" autocomplete="off" spellcheck="false">&lt;script&gt;&#xd;&#xa;function changeColor&#x28;&#x29;&#x7b;&#xd;&#xa; var newColor &#x3d; document.getElementById&#x28;&#x27;colorPicker&#x27;&#x29;.value&#x3b;&#xd;&#xa;&#x9;document.getElementById&#x28;&#x27;colorMsg&#x27;&#x29;.style.background &#x3d; newColor&#x3b;&#xd;&#xa;&#x7d;&#xd;&#xa;&lt;&#x2f;script&gt;&#xd;&#xa;&lt;div id&#x3d;&quot;colorMsg&quot; style&#x3d;&quot;font-size&#x3a;18px&#x3b;width&#x3a;150px&#x3b;height&#x3a;100px&#x3b;padding&#x3a;5px&#x3b;&quot;&gt;Choose a background color...&lt;&#x2f;div&gt; &#xd;&#xa;&lt;select id&#x3d;&quot;colorPicker&quot; onchange&#x3d;&quot;JavaScript&#x3a;changeColor&#x28;&#x29;&quot;&gt;&#xd;&#xa;&lt;option value&#x3d;&quot;transparent&quot;&gt;None&lt;&#x2f;option&gt;&#xd;&#xa;&lt;option value&#x3d;&quot;yellow&quot;&gt;Yellow&lt;&#x2f;option&gt;&#xd;&#xa;&lt;option value&#x3d;&quot;salmon&quot;&gt;Salmon&lt;&#x2f;option&gt;&#xd;&#xa;&lt;option value&#x3d;&quot;lightblue&quot;&gt;Light Blue&lt;&#x2f;option&gt;&#xd;&#xa;&lt;option value&#x3d;&quot;limegreen&quot;&gt;Lime Green&lt;&#x2f;option&gt;&#xd;&#xa;&lt;&#x2f;select&gt;</textarea> </div> <script> var exampleCode5 = CodeMirror.fromTextArea(document.getElementById("example5"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <ul class="pager"> <li><a href="/javascript/tutorial/two_dimensional_arrays.cfm"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> 2D Arrays</a></li> <li><a href="/javascript/tutorial/unobtrusive_javascript.cfm">Unobtrusive JavaScript <i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></li> </ul> </article> <div class="sidebar"> <nav> <ul> <li> <h3><a href="/javascript/tutorial/">JavaScript Tutorial</a></h3> <ul> <li><a href="/javascript/tutorial/introduction.cfm">Introduction</a></li> <li><a href="/javascript/tutorial/how_to_enable_javascript.cfm">How to Enable JavaScript</a></li> <li><a href="/javascript/tutorial/javascript_syntax.cfm">JavaScript Syntax</a></li> <li><a href="/javascript/tutorial/javascript_popup_boxes.cfm">JavaScript Popup Boxes</a></li> <li><a href="/javascript/tutorial/javascript_html.cfm">JavaScript and HTML</a></li> <li><a href="/javascript/tutorial/external_javascript_file.cfm">External JavaScript File</a></li> <li><a href="/javascript/tutorial/javascript_operators.cfm">JavaScript Operators</a></li> <li><a href="/javascript/tutorial/javascript_variables.cfm">JavaScript Variables</a></li> <li><a href="/javascript/tutorial/javascript_functions.cfm">JavaScript Functions</a></li> <li><a href="/javascript/tutorial/javascript_events.cfm">JavaScript Events</a></li> <li><a href="/javascript/tutorial/javascript_if_statements.cfm">JavaScript If Statements</a></li> <li><a href="/javascript/tutorial/javascript_switch_statement.cfm">JavaScript Switch Statements</a></li> <li><a href="/javascript/tutorial/javascript_while_loop.cfm">JavaScript While Loop</a></li> <li><a href="/javascript/tutorial/javascript_for_loop.cfm">JavaScript For Loop</a></li> <li><a href="/javascript/tutorial/javascript_try_catch.cfm">JavaScript Try Catch</a></li> <li><a href="/javascript/tutorial/javascript_escape_characters.cfm">JavaScript Escape Characters</a></li> <li><a href="/javascript/tutorial/javascript_void_0.cfm">JavaScript Void(0)</a></li> <li><a href="/javascript/tutorial/javascript_cookies.cfm">JavaScript Cookies</a></li> <li><a href="/javascript/tutorial/javascript_date_and_time.cfm">JavaScript Date and Time</a></li> <li><a href="/javascript/tutorial/javascript_arrays.cfm">JavaScript Arrays</a></li> <li><a href="/javascript/tutorial/two_dimensional_arrays.cfm">Two Dimensional Arrays</a></li> <li><a href="/javascript/tutorial/innerhtml_in_javascript.cfm">JavaScript innerHTML</a></li> <li><a href="/javascript/tutorial/unobtrusive_javascript.cfm">Unobtrusive JavaScript</a></li> <li><a href="/javascript/tutorial/javascript_summary.cfm">JavaScript Summary</a></li> </ul> </li> <li><h3>JavaScript Reference</h3> <ul> <li><a href="/javascript/examples/" title="">JavaScript Examples</a></li> <li><a href="/javascript/javascript_reserved_words.cfm" title="">JavaScript Reserved Words</a></li> <li><a href="/javascript/javascript_event_handlers.cfm" title="Introduction to the 12 JavaScript event handlers">JavaScript Event Handlers</a></li> <li><a href="/javascript/javascript_date_and_time_functions.cfm" title="Full listing of all JavaScript methods for dealing with date and time.">JavaScript Date and Time Functions</a></li> </ul> <li><h3>jQuery</h3> <ul> <li><a href="/jquery/tutorial/" title="">jQuery Tutorial</a></li> <li><a href="/jquery/examples/" title="">jQuery Examples</a></li> </ul> </li> <li><h3>JSON</h3> <ul> <li><a href="/json/tutorial/" title="Data format for exchanging data between applications and different environments.">JSON Tutorial</a></li> </ul> <li> </ul> </nav> <div class="ad ad-left"> <!-- GAM 71161633/QCKIT_quackit/article_vrec_2 --> <div data-fuse="23059511712"></div> </div> </div> <div class="ads"> <div class="ad ad-right"> <!-- GAM 71161633/QCKIT_quackit/article_vrec_1 --> <div data-fuse="23059883626"></div> </div> </div> </div> <div class="searchbox-bottom"> <form action="/search/" id="cse-search-box-bottom" class="site-search"> <div> <input type="hidden" name="cx" value="partner-pub-6331358926293806:npmuvy-i8kk"> <input type="hidden" name="cof" value="FORID:10"> <input type="hidden" name="ie" value="ISO-8859-1"> <input type="text" name="q" size="30" class="site-search-input"> <button type="submit" name="sa" class="site-search-button"><i class="fa fa-search"></i></button> </div> </form> <script src="//cse.google.com/cse/brand?form=cse-search-box-bottom&amp;lang=en"></script> </div> <footer> <p class="about"><a href="/"><i class="fa fa-home"></i> Home</a> | <a href="/about.cfm" rel="nofollow">About</a> | <a href="/contact.cfm" rel="nofollow">Contact</a> | <a href="/terms_of_use.cfm" rel="nofollow">Terms&nbsp;of&nbsp;Use</a> | <a href="/privacy_policy.cfm" rel="nofollow">Privacy&nbsp;Policy</a></p> <p>&#169; Copyright 2000 - 2024 Quackit.com &nbsp;</p> </footer> <script src="/common/js/spectrum/spectrum.js"></script> <script src="/common/js/lightbox2-master/dist/js/lightbox.min.js" charset="utf-8"></script> <script> $(document).ready(function(){ $( "#site-nav-toggler" ).click(function() { $( "#site-nav" ).toggle( "slow" ); }); }); </script> <script> $(function(){var a=window.location.href;$(".sidebar nav a").each(function(){a==this.href&&$(this).closest("li").addClass("selected")})}); </script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10