{"id":607,"date":"2021-05-17T13:15:07","date_gmt":"2021-05-17T11:15:07","guid":{"rendered":"http:\/\/strawberrycode.com\/blog\/?p=607"},"modified":"2023-05-17T15:14:03","modified_gmt":"2023-05-17T13:14:03","slug":"get-element-starting-with-class-in-js","status":"publish","type":"post","link":"http:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/","title":{"rendered":"How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors"},"content":{"rendered":"\n<p>You certainly know <code>*<\/code> and <code>^<\/code> as selectors in different programming languages. But you may have found some surprising (or frustrating) results while using them as JavaScript or jQuery selectors.<\/p>\n\n\n\n<p>Let&#8217;s say you want to access elements starting with a certain class name, for example, <em>&#8220;fruit-&#8220;<\/em> in the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div class=\"basket-item fruit-apple\">&#x1f34f;&lt;\/div><\/pre>\n\n\n\n<h2>First, the syntax<\/h2>\n\n\n\n<p>Your first reflex may be to try something like that:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Incorrect syntax\n$('.^fruit-').doSomething(...) \/\/ Nope, that won't work...<\/pre>\n\n\n\n<p>The correct syntax is:  <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Correct syntax\n$('[class^=\"fruit-\"]').doSomething(...)<\/pre>\n\n\n\n<h2>Then, the selectors: ^ vs *<\/h2>\n\n\n\n<p>Using the <code>^<\/code> selector is correct, but its behavior is a bit too literal: it will get the very first element starting with the class name, or more specifically, the class starting with the selected name. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ will match &lt;div class=\"fruit-strawberry basket-item\">&#x1f353;&lt;\/div>\n$('[class^=\"fruit-\"]').doSomething(...)  \n\n\/\/ won't match &lt;div class=\"basket-item fruit-apple\">&#x1f34f;&lt;\/div>\n$('[class^=\"fruit-\"]').doSomething(...)  <\/pre>\n\n\n\n<p>To match a class name starting with <em>&#8220;fruit-&#8220;<\/em> anywhere in the class string, you will need to use the * selector: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ will match &lt;div class=\"vegetable-carrot basket-item\">&#x1f955;&lt;\/div>\n$('[class*=\"vegetable-\"]').doSomething(...)\n\n\/\/ will also match &lt;div class=\"basket-item vegetable-potato\">&#x1f954;&lt;\/div>\n$('[class*=\"vegetable-\"]').doSomething(...)  <\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is a full example to play with on <a href=\"https:\/\/codepen.io\/strawberrycode\/pen\/JjWKgZp\" target=\"_blank\" rel=\"noreferrer noopener\">CodePen<\/a>: <\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_JjWKgZp\" src=\"\/\/codepen.io\/anon\/embed\/JjWKgZp?height=350&amp;theme-id=1&amp;slug-hash=JjWKgZp&amp;default-tab=result\" height=\"350\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed JjWKgZp\" title=\"CodePen Embed JjWKgZp\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I hope you found it useful. Feel free to <a href=\"https:\/\/www.buymeacoffee.com\/strawberrycode\" target=\"_blank\" rel=\"noreferrer noopener\">buy me a coffee (or tea)<\/a> to support the blog &#x1f642;<br>Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You certainly know * and ^ as selectors in different programming languages. But you may have found some surprising (or frustrating) results while using them as JavaScript or jQuery selectors. Let&#8217;s say you want to access elements starting with a certain class name, for example, &#8220;fruit-&#8220; in the following code: First, the syntax Your first reflex may be to try something like that: The correct syntax is: Then, the selectors: ^ vs * Using the ^ selector is correct, but its behavior is a bit too literal: it will get the very first element starting with the class name, or &hellip; <a href=\"http:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[115],"tags":[120,119,117,116],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors - StrawberryCode<\/title>\n<meta name=\"description\" content=\"How to get an element with a class starting with something using javascript selector * or ^\" \/>\n<link rel=\"canonical\" href=\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors - StrawberryCode\" \/>\n<meta property=\"og:description\" content=\"How to get an element with a class starting with something using javascript selector * or ^\" \/>\n<meta property=\"og:url\" content=\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\" \/>\n<meta property=\"og:site_name\" content=\"StrawberryCode\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-17T11:15:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-17T13:14:03+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/05\/get-element-starting-with-class-in-js.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"StrawberryCode\",\"description\":\"Fruit for Thought\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/strawberrycode.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/#primaryimage\",\"inLanguage\":\"en-GB\",\"url\":\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/05\/get-element-starting-with-class-in-js.png\",\"contentUrl\":\"http:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/05\/get-element-starting-with-class-in-js.png\",\"width\":1280,\"height\":720,\"caption\":\"Get element stating with class in js\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/#webpage\",\"url\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\",\"name\":\"How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors - StrawberryCode\",\"isPartOf\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/#primaryimage\"},\"datePublished\":\"2021-05-17T11:15:07+00:00\",\"dateModified\":\"2023-05-17T13:14:03+00:00\",\"author\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\"},\"description\":\"How to get an element with a class starting with something using javascript selector * or ^\",\"breadcrumb\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/get-element-starting-with-class-in-js\/\",\"name\":\"How to get an Element Starting with Class Name: the * and ^ JavaScript Selectors\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\",\"name\":\"StrawberryCode\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/607"}],"collection":[{"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/comments?post=607"}],"version-history":[{"count":7,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":681,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions\/681"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media\/626"}],"wp:attachment":[{"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}