{"componentChunkName":"component---src-templates-documentation-tsx","path":"/docs/handbook/dom-manipulation.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/intro.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/declaration-files/dts-from-js.html"},{"path":"/docs/handbook/intro-to-js-ts.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/release-notes/typescript-3-9.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/babel-with-typescript.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/vo/docs/handbook/basic-types.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/pt/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/ja/play"},{"path":"/pt/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/fa/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/4-0/new-checks/class-constructor-code-flow.ts"},{"path":"/play/4-0/new-js-features/jsdoc-deprecated.ts"},{"path":"/play/4-0/new-js-features/logical-operators-and-assignment.ts"},{"path":"/play/4-0/new-js-features/nullish-coalescing.ts"},{"path":"/play/4-0/new-ts-features/named-tuples.ts"},{"path":"/play/4-0/new-ts-features/unknown-in-catch.ts"},{"path":"/play/4-0/new-ts-features/variadic-tuples.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/language/soundness.ts"},{"path":"/es/play/typescript/language/structural-typing.ts"},{"path":"/es/play/typescript/language/type-guards.ts"},{"path":"/es/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/es/play/typescript/meta-types/conditional-types.ts"},{"path":"/es/play/typescript/meta-types/discriminate-types.ts"},{"path":"/es/play/typescript/meta-types/indexed-types.ts"},{"path":"/es/play/typescript/meta-types/mapped-types.ts"},{"path":"/es/play/typescript/primitives/any.ts"},{"path":"/es/play/typescript/primitives/literals.ts"},{"path":"/es/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/es/play/typescript/primitives/unknown-and-never.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/fa/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/ja/play/3-7/fixits/big-number-literals.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/ja/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/ja/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/primitives/unknown-and-never.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/ja/play/typescript/type-primitives/nullable-types.ts"},{"path":"/ja/play/typescript/type-primitives/tuples.ts"},{"path":"/pt/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/pt/play/javascript/javascript-essentials/functions.ts"},{"path":"/pt/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/pt/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/ja/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/ja/play/3-7/fixits/const-to-let.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/bootstrap"},{"path":"/es/docs/bootstrap"},{"path":"/ja/docs/bootstrap"},{"path":"/vo/docs/bootstrap"},{"path":"/zh/docs/bootstrap"},{"path":"/docs/"},{"path":"/es/docs/"},{"path":"/ja/docs/"},{"path":"/vo/docs/"},{"path":"/zh/docs/"},{"path":"/docs/handbook/"},{"path":"/es/docs/handbook/"},{"path":"/ja/docs/handbook/"},{"path":"/vo/docs/handbook/"},{"path":"/zh/docs/handbook/"},{"path":"/branding/"},{"path":"/dev/bug-workbench/"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"},{"path":"/upcoming/"}]},"markdownRemark":{"id":"a716d185-6e13-5b66-ade0-b82d0f469bb5","excerpt":"DOM Manipulation An exploration into the HTMLElement type In the 20+ years since its standardization, JavaScript has come a very long way. While in 202…","html":"<h2 id=\"dom-manipulation\" style=\"position:relative;\"><a href=\"#dom-manipulation\" aria-label=\"dom manipulation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>DOM Manipulation</h2>\n<h3 id=\"an-exploration-into-the-htmlelement-type\" style=\"position:relative;\"><a href=\"#an-exploration-into-the-htmlelement-type\" aria-label=\"an exploration into the htmlelement type permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><em>An exploration into the <code>HTMLElement</code> type</em></h3>\n<p>In the 20+ years since its standardization, JavaScript has come a very long way. While in 2020, JavaScript can be used on servers, in data science, and even on IoT devices, it is important to remember its most popular use case: web browsers.</p>\n<p>Website are made up of HTML and/or XML documents. These documents are static, they do not change. The D<em>ocument Object Model (DOM) is</em> a programming interface implemented by browsers in order to make static websites functional. The DOM API can be used to change the document structure, style, and content. The API is so powerful that countless frontend frameworks (jQuery, React, Angular, etc.) have been developed around it in order to make dynamic websites even easier to develop.</p>\n<p>TypeScript is a typed superset of JavaScript, and it ships type definitions for the DOM API. These definitions are readily available in any default TypeScript project. Of the 20,000+ lines of definitions in <em>lib.dom.d.ts</em>, one stands out among the rest: <code>HTMLElement</code> . This type is the backbone for DOM manipulation with TypeScript.</p>\n<blockquote>\n<p>You can explore the source code for the <a href=\"https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts\">DOM type definitions</a></p>\n</blockquote>\n<h2 id=\"basic-example\" style=\"position:relative;\"><a href=\"#basic-example\" aria-label=\"basic example permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Basic Example</h2>\n<p>Given a simplified <em>index.html</em> file:</p>\n<pre><code>&#x3C;!DOCTYPE html>\n&#x3C;html lang=\"en\">\n  &#x3C;head>&#x3C;title>TypeScript Dom Manipulation&#x3C;/title>&#x3C;/head>\n  &#x3C;body>\n    &#x3C;div id=\"app\">&#x3C;/div>\n        &#x3C;!-- Assume index.js is the compiled output of index.ts -->\n    &#x3C;script src=\"index.js\">&#x3C;/script>\n  &#x3C;/body>\n&#x3C;/html>\n</code></pre>\n<p>Lets explore a TypeScript script that adds a <code>&#x3C;p>Hello, World&#x3C;/p></code> element to the <code>#app</code> element.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">// 1. Select the div element using the id property</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">app </span><span style=\"color: #000000\">= document.getElementById(</span><span style=\"color: #A31515\">\"app\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #008000\">// 2. Create a new &lt;p&gt;&lt;/p&gt; element programmatically</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">p </span><span style=\"color: #000000\">= document.createElement(</span><span style=\"color: #A31515\">\"p\"</span><span style=\"color: #000000\">);</span>\n\n<span style=\"color: #008000\">// 3. Add the text content</span>\n<span style=\"color: #1A1A1A\">p</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">textContent</span><span style=\"color: #000000\"> = </span><span style=\"color: #A31515\">\"Hello, World!\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">// 4. Append the p element to the div element</span>\n<span style=\"color: #1A1A1A\">app</span><span style=\"color: #000000\">?.appendChild(</span><span style=\"color: #1A1A1A\">p</span><span style=\"color: #000000\">);</span></code></div></pre>\n<p>After compiling and running the <em>index.html</em> page, the resulting HTML will be:</p>\n<pre class=\"shiki\"><div class=\"language-id\">html</div><div class='code-container'><code><span style=\"color: #800000\">&lt;div</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">id</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"app\"</span><span style=\"color: #800000\">&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;p&gt;</span><span style=\"color: #000000\">Hello, World!</span><span style=\"color: #800000\">&lt;/p&gt;</span>\n<span style=\"color: #800000\">&lt;/div&gt;</span></code></div></pre>\n<h2 id=\"the-document-interface\" style=\"position:relative;\"><a href=\"#the-document-interface\" aria-label=\"the document interface permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code>Document</code> Interface</h2>\n<p>The first line of the TypeScript code uses a global variable <code>document</code> , inspecting the variable shows it is defined by the <code>Document</code> interface from the <em>lib.dom.d.ts</em> file. The code snippet contains calls to two methods, <code>getElementById</code> and <code>createElement</code>.</p>\n<h3 id=\"documentgetelementbyid\" style=\"position:relative;\"><a href=\"#documentgetelementbyid\" aria-label=\"documentgetelementbyid permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code>Document.getElementById</code></h3>\n<p>The definition for this method is as follows:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">getElementById(</span><span style=\"color: #1A1A1A\">elementId</span><span style=\"color: #000000\">: </span><span style=\"color: #1A1A1A\">string</span><span style=\"color: #000000\">): HTMLElement | </span><span style=\"color: #0000FF\">null</span><span style=\"color: #000000\">;</span></code></div></pre>\n<p>Pass it an element id string and it will return either <code>HTMLElement</code> or <code>null</code> . This method introduces one of the most important types, <code>HTMLElement</code>. It serves as the base interface for every other element interface. For example, the <code>p</code> variable in the code example is of type <code>HTMLParagraphElement</code>. Also take note that this method can return <code>null</code>. This is because the method can’t be certain pre-runtime if it will be able to actually find the specified element or not. In the last line of the code snippet, the new <em>optional chaining</em> operator is used in order to call <code>appendChild</code>.</p>\n<h3 id=\"documentcreateelement\" style=\"position:relative;\"><a href=\"#documentcreateelement\" aria-label=\"documentcreateelement permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code>Document.createElement</code></h3>\n<p>The definition for this method is (I have omitted the <em>deprecated</em> definition):</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">createElement&lt;K extends </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> HTMLElementTagNameMap&gt;(</span><span style=\"color: #1A1A1A\">tagName</span><span style=\"color: #000000\">: K, </span><span style=\"color: #1A1A1A\">options</span><span style=\"color: #000000\">?: </span><span style=\"color: #1A1A1A\">ElementCreationOptions</span><span style=\"color: #000000\">): </span><span style=\"color: #1A1A1A\">HTMLElementTagNameMap</span><span style=\"color: #000000\">[K];</span>\n<span style=\"color: #000000\">createElement(</span><span style=\"color: #1A1A1A\">tagName</span><span style=\"color: #000000\">: </span><span style=\"color: #1A1A1A\">string</span><span style=\"color: #000000\">, </span><span style=\"color: #1A1A1A\">options</span><span style=\"color: #000000\">?: </span><span style=\"color: #1A1A1A\">ElementCreationOptions</span><span style=\"color: #000000\">): HTMLElement;</span></code></div></pre>\n<p>This is an overloaded function definition. The second overload is simplest and works a lot like the <code>getElementById</code> method does. Pass it any <code>string</code> and it will return a standard HTMLElement. This definition is what enables developers to create unique HTML element tags.</p>\n<p>For example <code>document.createElement('xyz')</code> returns a <code>&#x3C;xyz>&#x3C;/xyz></code> element, clearly not an element that is specified by the HTML specification.</p>\n<blockquote>\n<p>For those interested, you can interact with custom tag elements using the <code>document.getElementsByTagName</code></p>\n</blockquote>\n<p>For the first definition of <code>createElement</code>, it is using some advanced generic patterns. It is best understood broken down into chunks. Starting with the generic expression: <code>&#x3C;K extends keyof HTMLElementTagNameMap></code>. This expression defines a generic parameter <code>K</code> that is <em>constrained</em> to the keys of the interface <code>HTMLElementTagNameMap</code>. The map interface contains every specified HTML tag name and its corresponding type interface. For example here are the first 5 mapped values:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> HTMLElementTagNameMap {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"a\"</span><span style=\"color: #000000\">: HTMLAnchorElement;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"abbr\"</span><span style=\"color: #000000\">: HTMLElement;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"address\"</span><span style=\"color: #000000\">: HTMLElement;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"applet\"</span><span style=\"color: #000000\">: HTMLAppletElement;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"area\"</span><span style=\"color: #000000\">: HTMLAreaElement;</span>\n<span style=\"color: #000000\">        ...</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Some elements do not exhibit unique properties and so they just return <code>HTMLElement</code>, but other types do have unique properties and methods so they return their specific interface (which will extend from or implement <code>HTMLElement</code>).</p>\n<p>Now, for the remainder of the <code>createElement</code> definition: <code>(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K]</code>. The first argument <code>tagName</code> is defined as the generic parameter <code>K</code> . The TypeScript interpreter is smart enough to <em>infer</em> the generic parameter from this argument. This means that the developer does not actually have to specify the generic parameter when using the method; whatever value is passed to the <code>tagName</code> argument will be inferred as <code>K</code> and thus can be used throughout the remainder of the definition. Which is exactly what happens; the return value <code>HTMLElementTagNameMap[K]</code> takes the <code>tagName</code> argument and uses it to return the corresponding type. This definition is how the <code>p</code> variable from the code snippet gets a type of <code>HTMLParagraphElement</code>. And if the code was <code>document.createElement('a')</code>, then it would be a element of type <code>HTMLAnchorElement</code>.</p>\n<h2 id=\"the-node-interface\" style=\"position:relative;\"><a href=\"#the-node-interface\" aria-label=\"the node interface permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code>Node</code> interface</h2>\n<p>The <code>document.getElementById</code> function returns an <code>HTMLElement</code>. <code>HTMLElement</code> interface extends the <code>Element</code> interface which extends the <code>Node</code> interface. This prototypal extension allows for all <code>HTMLElements</code> to utilize a subset of standard methods. In the code snippet, we use a property defined on the <code>Node</code> interface to append the new <code>p</code> element to the website.</p>\n<h3 id=\"nodeappendchild\" style=\"position:relative;\"><a href=\"#nodeappendchild\" aria-label=\"nodeappendchild permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a><code>Node.appendChild</code></h3>\n<p>The last line of the code snippet is <code>app?.appendChild(p)</code>. The previous, <code>document.getElementById</code> , section detailed that the <em>optional chaining</em> operator is used here because <code>app</code> can potentially be null at runtime. The <code>appendChild</code> method is defined by:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #000000\">appendChild&lt;T extends Node&gt;(</span><span style=\"color: #1A1A1A\">newChild</span><span style=\"color: #000000\">: T): T;</span></code></div></pre>\n<p>This method works similarly to the <code>createElement</code> method as the generic parameter <code>T</code> is inferred from the <code>newChild</code> argument. <code>T</code> is <em>constrained</em> to another base interface <code>Node</code>.</p>\n<h2 id=\"difference-between-children-and-childnodes\" style=\"position:relative;\"><a href=\"#difference-between-children-and-childnodes\" aria-label=\"difference between children and childnodes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Difference between <code>children</code> and <code>childNodes</code></h2>\n<p>Previously, this document details the <code>HTMLElement</code> interface extends from <code>Element</code> which extends from <code>Node</code>. In the DOM API there is a concept of <em>children</em> elements. For example in the following HTML, the <code>p</code> tags are children of the <code>div</code> element</p>\n<pre class=\"shiki\"><div class=\"language-id\">tsx</div><div class='code-container'><code><span style=\"color: #800000\">&lt;div&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;p&gt;</span><span style=\"color: #000000\">Hello, World</span><span style=\"color: #800000\">&lt;/p&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;p&gt;</span><span style=\"color: #000000\">TypeScript!</span><span style=\"color: #800000\">&lt;/p&gt;</span>\n<span style=\"color: #800000\">&lt;/div&gt;</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\"> = document.getElementByTagName(</span><span style=\"color: #A31515\">\"div\"</span><span style=\"color: #000000\">)[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">children</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #008000\">// HTMLCollection(2) [p, p]</span>\n\n<span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\">.childNodes;</span>\n<span style=\"color: #008000\">// NodeList(2) [p, p]</span></code></div></pre>\n<p>After capturing the <code>div</code> element, the <code>children</code> prop will return a <code>HTMLCollection</code> list containing the <code>HTMLParagraphElements</code>. The <code>childNodes</code> property will return a similar <code>NodeList</code> list of nodes. Each <code>p</code> tag will still be of type <code>HTMLParagraphElements</code>, but the <code>NodeList</code> can contain additional <em>HTML nodes</em> that the <code>HTMLCollection</code> list cannot.</p>\n<p>Modify the html by removing one of the <code>p</code> tags, but keep the text.</p>\n<pre class=\"shiki\"><div class=\"language-id\">tsx</div><div class='code-container'><code><span style=\"color: #800000\">&lt;div&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;p&gt;</span><span style=\"color: #000000\">Hello, World</span><span style=\"color: #800000\">&lt;/p&gt;</span>\n<span style=\"color: #000000\">  TypeScript!</span>\n<span style=\"color: #800000\">&lt;/div&gt;</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\"> = document.getElementByTagName(</span><span style=\"color: #A31515\">\"div\"</span><span style=\"color: #000000\">)[</span><span style=\"color: #09835A\">0</span><span style=\"color: #000000\">];</span>\n\n<span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\">.</span><span style=\"color: #1A1A1A\">children</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #008000\">// HTMLCOllection(1) [p]</span>\n\n<span style=\"color: #1A1A1A\">div</span><span style=\"color: #000000\">.childNodes;</span>\n<span style=\"color: #008000\">// NodeList(2) [p, text]</span></code></div></pre>\n<p>See how both lists change. <code>children</code> now only contains the <code>&#x3C;p>Hello, World&#x3C;/p></code> element, and the <code>childNodes</code> contains a <code>text</code> node rather than two <code>p</code> nodes. The <code>text</code> part of the <code>NodeList</code> is the literal <code>Node</code> containing the text <code>TypeScript!</code>. The <code>children</code> list does not contain this <code>Node</code> because it is not considered an <code>HTMLElement</code>.</p>\n<h2 id=\"the-queryselector-and-queryselectorall-methods\" style=\"position:relative;\"><a href=\"#the-queryselector-and-queryselectorall-methods\" aria-label=\"the queryselector and queryselectorall methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The <code>querySelector</code> and <code>querySelectorAll</code> methods</h2>\n<p>Both of these methods are great tools for getting lists of dom elements that fit a more unique set of constraints. They are defined in <em>lib.dom.d.ts</em> as:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #008000\">/**</span>\n<span style=\"color: #008000\"> * Returns the first element that is a descendant of node that matches selectors.</span>\n<span style=\"color: #008000\"> */</span>\n<span style=\"color: #000000\">querySelector&lt;K extends </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> HTMLElementTagNameMap&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: K): </span><span style=\"color: #1A1A1A\">HTMLElementTagNameMap</span><span style=\"color: #000000\">[K] | </span><span style=\"color: #0000FF\">null</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">querySelector&lt;K extends </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> SVGElementTagNameMap&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: K): </span><span style=\"color: #1A1A1A\">SVGElementTagNameMap</span><span style=\"color: #000000\">[K] | </span><span style=\"color: #0000FF\">null</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #000000\">querySelector&lt;E extends Element = Element&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: </span><span style=\"color: #1A1A1A\">string</span><span style=\"color: #000000\">): E | </span><span style=\"color: #0000FF\">null</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #008000\">/**</span>\n<span style=\"color: #008000\"> * Returns all element descendants of node that match selectors.</span>\n<span style=\"color: #008000\"> */</span>\n<span style=\"color: #000000\">querySelectorAll&lt;K extends </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> HTMLElementTagNameMap&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: K): </span><span style=\"color: #1A1A1A\">NodeListOf</span><span style=\"color: #000000\">&lt;</span><span style=\"color: #1A1A1A\">HTMLElementTagNameMap</span><span style=\"color: #000000\">[K]&gt;;</span>\n<span style=\"color: #000000\">querySelectorAll&lt;K extends </span><span style=\"color: #0000FF\">keyof</span><span style=\"color: #000000\"> SVGElementTagNameMap&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: K): </span><span style=\"color: #1A1A1A\">NodeListOf</span><span style=\"color: #000000\">&lt;</span><span style=\"color: #1A1A1A\">SVGElementTagNameMap</span><span style=\"color: #000000\">[K]&gt;;</span>\n<span style=\"color: #000000\">querySelectorAll&lt;E extends Element = Element&gt;(</span><span style=\"color: #1A1A1A\">selectors</span><span style=\"color: #000000\">: </span><span style=\"color: #1A1A1A\">string</span><span style=\"color: #000000\">): </span><span style=\"color: #1A1A1A\">NodeListOf</span><span style=\"color: #000000\">&lt;E&gt;;</span></code></div></pre>\n<p>The <code>querySelectorAll</code> definition is similar to <code>getElementByTagName</code>, except it returns a new type: <code>NodeListOf</code>. This return type is essentially a custom implementation of the standard JavaScript list element. Arguably, replacing <code>NodeListOf&#x3C;E></code> with <code>E[]</code> would result in a very similar user experience. <code>NodeListOf</code> only implements the following properties and methods: <code>length</code> , <code>item(index)</code>, <code>forEach((value, key, parent) => void)</code> , and numeric indexing. Additionally, this method returns a list of <em>elements</em> not <em>nodes</em>, which is what <code>NodeList</code> was returning from the <code>.childNodes</code> method. While this may appear as a discrepancy, take note that interface <code>Element</code> extends from <code>Node</code>.</p>\n<p>To see these methods in action modify the existing code to:</p>\n<pre class=\"shiki\"><div class=\"language-id\">tsx</div><div class='code-container'><code><span style=\"color: #800000\">&lt;ul&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;li&gt;</span><span style=\"color: #000000\">First :)</span><span style=\"color: #800000\">&lt;/li&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;li&gt;</span><span style=\"color: #000000\">Second!</span><span style=\"color: #800000\">&lt;/li&gt;</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #800000\">&lt;li&gt;</span><span style=\"color: #000000\">Third times a charm.</span><span style=\"color: #800000\">&lt;/li&gt;</span>\n<span style=\"color: #800000\">&lt;/ul&gt;</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">first</span><span style=\"color: #000000\"> = document.querySelector(</span><span style=\"color: #A31515\">\"li\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// returns the first li element</span>\n<span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> </span><span style=\"color: #1A1A1A\">all</span><span style=\"color: #000000\"> = document.querySelectorAll(</span><span style=\"color: #A31515\">\"li\"</span><span style=\"color: #000000\">); </span><span style=\"color: #008000\">// returns the list of all li elements</span></code></div></pre>\n<h2 id=\"interested-in-learning-more\" style=\"position:relative;\"><a href=\"#interested-in-learning-more\" aria-label=\"interested in learning more permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Interested in learning more?</h2>\n<p>The best part about the <em>lib.dom.d.ts</em> type definitions is that they are reflective of the types annotated in the Mozilla Developer Network (MDN) documentation site. For example, the <code>HTMLElement</code> interface is documented by this <a href=\"https://developer.mozilla.org/docs/Web/API/HTMLElement\">HTMLElement page</a> on MDN. These pages list all available properties, methods, and sometimes even examples. Another great aspect of the pages is that they provide links to the corresponding standard documents. Here is the link to the <a href=\"https://www.w3.org/TR/html52/dom.html#htmlelement\">W3C Recommendation for HTMLElement</a>.</p>\n<p>Sources:</p>\n<ul>\n<li><a href=\"http://www.ecma-international.org/ecma-262/10.0/index.html\">ECMA-262 Standard</a></li>\n<li><a href=\"https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction\">Introduction to the DOM</a></li>\n</ul>","headings":[{"value":"DOM Manipulation","depth":2},{"value":"An exploration into the HTMLElement type","depth":3},{"value":"Basic Example","depth":2},{"value":"The Document Interface","depth":2},{"value":"Document.getElementById","depth":3},{"value":"Document.createElement","depth":3},{"value":"The Node interface","depth":2},{"value":"Node.appendChild","depth":3},{"value":"Difference between children and childNodes","depth":2},{"value":"The querySelector and querySelectorAll methods","depth":2},{"value":"Interested in learning more?","depth":2}],"frontmatter":{"permalink":"/docs/handbook/dom-manipulation.html","title":"DOM Manipulation","disable_toc":null,"oneline":"Using the DOM with TypeScript"}},"prev":null,"next":null},"pageContext":{"id":"dom-manipulation","slug":"/docs/handbook/dom-manipulation.html","repoPath":"/packages/documentation/copy/en/tutorials/DOM Manipulation.md","lang":"en","modifiedTime":"2020-08-07T17:00:36.347Z"}}}