{"id":244,"date":"2018-01-13T17:42:00","date_gmt":"2018-01-13T17:42:00","guid":{"rendered":"https:\/\/the-js-source.flywheelsites.com\/?p=244"},"modified":"2021-07-28T23:14:22","modified_gmt":"2021-07-28T23:14:22","slug":"getelementsbyattribute","status":"publish","type":"post","link":"https:\/\/javascriptsource.com\/getelementsbyattribute\/","title":{"rendered":"getElementsByAttribute"},"content":{"rendered":"\n
Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this code snippet to your script and your problem will be solved.<\/p>\n\n\n\n
<!\u2013 Paste this snippet into your existing code \u2013>\n\n\n\/* This script and many more are available free online at\n The JavaScript Source :: http:\/\/javascriptsource.com \n Copyright Robert Nyman, http:\/\/www.robertnyman.com\n Free to use if this text is included *\/\n\nfunction getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){\n var arrElements = (strTagName == \u201c*\u201d && document.all)? document.all : oElm.getElementsByTagName(strTagName);\n var arrReturnElements = new Array();\n var oAttributeValue = (typeof strAttributeValue != \u201cundefined\u201d)? new RegExp(\u201c(^|\\s)\u201d + strAttributeValue + \u201c(\\s|$)\u201d) : null;\n var oCurrent;\n var oAttribute;\n for(var i=0; i<arrElements.length; i++){\n oCurrent = arrElements[i];\n oAttribute = oCurrent.getAttribute(strAttributeName);\n if(typeof oAttribute == \u201cstring\u201d && oAttribute.length > 0){\n if(typeof strAttributeValue == \u201cundefined\u201d || (oAttributeValue && oAttributeValue.test(oAttribute))){\n arrReturnElements.push(oCurrent);\n }\n }\n }\n return arrReturnElements;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this code snippet to your script and your problem will be solved.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-244","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-addon","7":"entry"},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"JavaScriptSource Editor","author_link":"https:\/\/javascriptsource.com\/author\/bkmacdaddybsa\/"},"_links":{"self":[{"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":2,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":338,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/posts\/244\/revisions\/338"}],"wp:attachment":[{"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javascriptsource.com\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}