{"id":18695,"date":"2020-06-30T11:10:01","date_gmt":"2020-06-30T18:10:01","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=18695"},"modified":"2023-12-01T03:36:48","modified_gmt":"2023-12-01T11:36:48","slug":"queryselector-javascript","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/","title":{"rendered":"querySelector JavaScript: A Guide"},"content":{"rendered":"\n<p><code>getElementById<\/code>. <code>getElementByClass<\/code>. You may have heard about these functions before. They both allow you to select a particular element in JavaScript.<br><\/p>\n\n\n\n<p>Once you\u2019ve selected an element you can change its contents, modify its state, or even make it disappear.<br><\/p>\n\n\n\n<p>There\u2019s another function you can use to select an item on a webpage in JavaScript: <code>querySelector<\/code>. This function has a sister called <code>querySelectorAll<\/code>, which returns all of the elements that meet a particular query.<br><\/p>\n\n\n\n<p>In this guide, we\u2019re going to discuss how to use the <code>querySelector<\/code> and <code>querySelectorAll<\/code> methods. We\u2019ll walk through an example of these methods in action so that you\u2019ll be ready to use them in your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is querySelector?<\/h2>\n\n\n\n<p><code>querySelector<\/code> is a function that returns an element that matches a CSS query. Note the <code>querySelector<\/code> method only returns the first instance of a selector. When using <code>querySelector<\/code>, you can specify any CSS selector that you want. For instance, you could retrieve an element which has a specific ID set, or an element by its tag name.<br><\/p>\n\n\n\n<p>You can use this function like this:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>var elementName = document.querySelector('#thisElement');<\/pre><\/div>\n\n\n\n<p>We\u2019ve specified the CSS selectors to match in quotation marks. In addition, you can use the method to select an element within another element:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>var parentElement = document.querySelector('main');\nvar childElement = document.querySelector('#childElement');<\/pre><\/div>\n\n\n\n<p>This will return the element with the ID #childElement which is contained within the &lt;main&gt; tag in our HTML file.&nbsp;<br><\/p>\n\n\n\n<p>The <code>querySelectorAll<\/code> method works in the same way. The difference is that it returns all the matches found by the query selector, rather than just the first one:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>var allParagraphs = document.querySelectorAll('p');<\/pre><\/div>\n\n\n\n<p>This will return a list of all the &lt;p&gt; tags on an HTML page.<br><\/p>\n\n\n\n<p>Here are a few examples of selectors you could use with this function:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>p: Selects all &lt;p&gt; tags on the page.<\/li>\n\n\n\n<li>#name_field: Selects the element with the id \u201cname_field\u201d<\/li>\n\n\n\n<li>.list: Selects all elements with the class name \u201clist\u201d<\/li>\n\n\n\n<li>div &gt; p: Selects all &lt;p&gt; elements contained within a &lt;div&gt; element.<\/li>\n<\/ul>\n\n\n\n<p>You can specify multiple selectors using the <code>querySelector<\/code> and <code>querySelectorAll<\/code> methods. To learn more about CSS selectors, check out our guide to CSS and HTML <a href=\"https:\/\/careerkarma.com\/blog\/css-attribute-selector\/\">attribute selectors<\/a>.<br><\/p>\n\n\n\n<p>If matches are not found, null is returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use Query Selector<\/h2>\n\n\n\n<p>Let\u2019s build a simple web application to illustrate how the <code>querySelector<\/code> method works. We\u2019re going to create a web page that displays a fact about HTML. We are going to use <code>querySelector<\/code> to add a button which, when clicked, will enable a \u201cdark mode\u201d.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up the Front End<\/h3>\n\n\n\n<p>We\u2019ll start by setting up the front-end for our page:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n\t&lt;head&gt;\n\t\t&lt;title&gt;HTML Facts!&lt;\/title&gt;\n\t\t&lt;link rel=\"stylesheet\" href=\".\/facts.css\"&gt;\n\t&lt;\/head&gt;\n\t&lt;body&gt;\n\t\t&lt;div class=\"mainBox\"&gt;\n\t\t\t&lt;h1 class=\"text\"&gt;HTML Facts!&lt;\/h1&gt;\n\t\t\t&lt;p class=\"text\"&gt;Did you know that HTML is actually based on another markup language, SGML, or Standard Generalized Markup Language?&lt;\/p&gt;\n\t\t\t&lt;button type=\"button\"&gt;Enable dark mode&lt;\/button&gt;\n\t\t&lt;\/div&gt;\n\t\t&lt;script src=\".\/facts.js\"&gt;&lt;\/script&gt;\n\t&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>We have created a simple HTML webpage that displays a fact about HTML. Right now, our page looks like this:<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/-8hMiRuo3rOf5CIAXCiSF22AxhbE4mar1kQTjHZrC8K_eMEEb0o24Q9QAxKiLsCn2w6BDHZYoYIh-2oQwvZdJ8uk02CeZv8nylX6EkchnU425WE2PJ2LhDLc-1XYg9skig4Pige9\" alt=\"\"\/><\/figure>\n\n\n\n<p>While our page does give our user a fact about HTML, it is missing two key features: styles and a functional dark mode button. Let\u2019s add the following styles to a file called facts.css:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>.mainBox {\n    margin: auto;\n    width: 50%;\n    text-align: center;\n    background: hotpink;\n    padding: 5%;\n}\nbody {\n    background: lightblue;\n}\nbutton {\n    padding: 10px;\n    background: lightgreen;\n    border-radius: 10px;\n    border: none;\n}<\/pre><\/div>\n\n\n\n<p>These styles make our site look more aesthetically pleasing:<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/AAQKzpsbXEJE_9i8asYZpF-KiAMP1E2IHl66ZeuhDepcCjiaUbAo11WjpEgo--3XAJ9WNl-KzXTFr8b-TrnUxA971fFvqyukSJkqexCRiBOgS3DX-4H6agqxFbe1Rm7qWkD1dHuZ\" alt=\"\"\/><\/figure>\n\n\n\n<p>That\u2019s all we need to do on the front-end. Now we\u2019re going to focus on adding our dark mode using JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Dark Mode Using JavaScript<\/h3>\n\n\n\n<p>Our dark mode function should include two features:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The heading and paragraph inside our box should be changed to white.<\/li>\n\n\n\n<li>The blue background of our site should be changed to black.<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s get started by selecting the DOM elements (webpage elements) that we want to target: the heading, the paragraph, and the main body of our page. We\u2019re going to do this in a file called facts.js:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>const heading = document.querySelector(\"h1\");\nconst paragraph = document.querySelector(\"p\");\nconst body = document.querySelector(\"body\");<\/pre><\/div>\n\n\n\n<p>This code returns the first element in the document that has the tag &lt;h1&gt;, &lt;p&gt;, and &lt;body&gt;Now we\u2019re going to create a function that changes the colors of our text and body tag. We\u2019ll change our heading and paragraph text to white and the background of our site to black.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function enableDarkMode() {\n\theading.setAttribute(\"style\", \"color: white;\");\n\tparagraph.setAttribute(\"style\", \"color: white;\");\n\tbody.setAttribute(\"style\", \"background-color: black;\");\n}<\/pre><\/div>\n\n\n\n<p>We\u2019re almost there. The next step is to set up an event listener. This listener will listen for whenever a user clicks our button. When the user clicks the button, the dark mode will be enabled:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>document.querySelector(\"button\").addEventListener(\"click\", enableDarkMode);<\/pre><\/div>\n\n\n\n<p>Now, let\u2019s see what happens when we click our button:<br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Yc5ZKxo3kenpvinjS47C3ispWFZdL55D8t845h0_UpGoVS24g8ECLr8pm5J-098M1ALM3wcGdmcoKPnXc9jg7N_QB7mOS1l2RMOy_bJCe9izeRLudsunSC7vbERP0pgp_JrdUlJ7\" alt=\"\"\/><\/figure>\n\n\n\n<p>Our site has successfully changed into a dark mode.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion (and Challenge)<\/h2>\n\n\n\n<p>You\u2019ll notice that our code only allows a user to enable dark mode. What gives? This presents a challenge for you: write the code that will allow a user to disable dark mode on the site.<br><\/p>\n\n\n\n<p>The <code>querySelector<\/code> method allows you to select HTML elements based on a CSS selector query. This method returns the first instance of a match. You can use the <code>querySelectorAll<\/code> method to retrieve a list of all the elements that match a specified selector<br><\/p>\n\n\n\n<p>Now you\u2019re ready to start using the <code>querySelector<\/code> method like a JavaScript pro!<\/p>\n","protected":false},"excerpt":{"rendered":"getElementById. getElementByClass. You may have heard about these functions before. They both allow you to select a particular element in JavaScript. Once you\u2019ve selected an element you can change its contents, modify its state, or even make it disappear. There\u2019s another function you can use to select an item on a webpage in JavaScript: querySelector.&hellip;","protected":false},"author":240,"featured_media":17390,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-18695","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>querySelector JavaScript: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"querySelector JavaScript: A Guide\" \/>\n<meta property=\"og:description\" content=\"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-30T18:10:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:36:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"679\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"querySelector JavaScript: A Guide\",\"datePublished\":\"2020-06-30T18:10:01+00:00\",\"dateModified\":\"2023-12-01T11:36:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/\"},\"wordCount\":796,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/christopher-gower-m_HRfLhgABo-unsplash.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/\",\"name\":\"querySelector JavaScript: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/christopher-gower-m_HRfLhgABo-unsplash.jpg\",\"datePublished\":\"2020-06-30T18:10:01+00:00\",\"dateModified\":\"2023-12-01T11:36:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/christopher-gower-m_HRfLhgABo-unsplash.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/05\\\/christopher-gower-m_HRfLhgABo-unsplash.jpg\",\"width\":1020,\"height\":679,\"caption\":\"a MacBook Pro on a desk with a phone, headphones, coffee mug, light, and a computer monitor next to it\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/queryselector-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"querySelector JavaScript: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"querySelector JavaScript: A Guide | Career Karma","description":"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/","og_locale":"en_US","og_type":"article","og_title":"querySelector JavaScript: A Guide","og_description":"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.","og_url":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-06-30T18:10:01+00:00","article_modified_time":"2023-12-01T11:36:48+00:00","og_image":[{"width":1020,"height":679,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"querySelector JavaScript: A Guide","datePublished":"2020-06-30T18:10:01+00:00","dateModified":"2023-12-01T11:36:48+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/"},"wordCount":796,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/","url":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/","name":"querySelector JavaScript: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg","datePublished":"2020-06-30T18:10:01+00:00","dateModified":"2023-12-01T11:36:48+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The querySelector and querySelectorAll methods allow you to select HTML elements in JavaScript using selectors. On Career Karma, learn how to use these methods.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/queryselector-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/christopher-gower-m_HRfLhgABo-unsplash.jpg","width":1020,"height":679,"caption":"a MacBook Pro on a desk with a phone, headphones, coffee mug, light, and a computer monitor next to it"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/queryselector-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"querySelector JavaScript: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/18695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=18695"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/18695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/17390"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=18695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=18695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=18695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}