{"id":6040,"date":"2025-06-05T01:52:04","date_gmt":"2025-06-05T06:52:04","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=6040"},"modified":"2025-06-05T01:52:06","modified_gmt":"2025-06-05T06:52:06","slug":"firstchild-can-be-white-space","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/firstchild-can-be-white-space\/","title":{"rendered":"firstChild can be white space"},"content":{"rendered":"\n<p>This is one of those things that bit me just the other day so I figured I&#8217;d write up. It&#8217;s a fairly simple concept and maybe it&#8217;s not something you&#8217;d be confused by, but like I said, it got me!<\/p>\n\n\n\n<p>What is the first child of the section here?<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>&lt;div&gt;<\/code> is the first child of the <code>&lt;section&gt;<\/code>, right? <a href=\"https:\/\/codepen.io\/chriscoyier\/pen\/GgJZmpv?editors=1012\">Wrong!<\/a> Well, if we&#8217;re talking about <code>.firstChild<\/code> in JavaScript, anyway. The actual <code>firstChild<\/code> here is what is called a &#8220;text node&#8221; which will have a value of <code>\"\\n \"<\/code>. It&#8217;s the white space! The return and space before the <code>&lt;div&gt;<\/code> hits. <\/p>\n\n\n\n<p class=\"has-small-font-size\">(These are just placeholder HTML elements for illustration, they could be any HTML elements.)<\/p>\n\n\n\n<p>If the HTML were like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><em>Then<\/em> the <code>firstChild<\/code> of the section would be the <code>&lt;div&gt;<\/code>.<\/p>\n\n\n\n<p>This same &#8220;problem&#8221; would be true if there was an HTML comment like <code>&lt;!-- this --&gt;<\/code> between the <code>&lt;section&gt;<\/code> and the <code>&lt;div&gt;<\/code>.<\/p>\n\n\n\n<p>If we wanted to ensure we were getting the first actual HTML element child, we could use a different method. We&#8217;d use <code>.firstElementChild<\/code> instead, which will skip over text nodes and get the first actual HTML element, if there is one. (That&#8217;s what helped me.)<\/p>\n\n\n\n<p>I guess I found it confusing as it&#8217;s normally not the case that you select text nodes. I don&#8217;t even think of them as &#8220;selectable&#8221; generally. For instance, <code>section :first-child<\/code> in CSS as a selector will certainly select the <code>&lt;div&gt;<\/code> not the white space. And if you used that selector in <code>querySelector<\/code> it would do the same. And if you were to look at the <code>.children<\/code> of either HTML scenario above, you&#8217;d find a single child that is the <code>&lt;div&gt;<\/code>. So <code>.children[0]<\/code>, which you could be forgiven for thinking would be equal to <code>.firstChild<\/code> are not indeed equal.<\/p>\n\n\n\n<p>Just a little thing you might need to know: the <code>.firstChild<\/code> method (and <code>.lastChild<\/code>!) will return a text node if there is white space (or, of course, other text) within an element.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a tiny gotcha. <\/p>\n","protected":false},"author":1,"featured_media":6051,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-6040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-javascript"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/06\/firstChild-can-be-white-space.jpg?fit=1140%2C676&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=6040"}],"version-history":[{"count":4,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6040\/revisions"}],"predecessor-version":[{"id":6052,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/6040\/revisions\/6052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/6051"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=6040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=6040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=6040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}