{"id":4306,"date":"2025-04-09T22:24:08","date_gmt":"2025-04-09T21:24:08","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=4306"},"modified":"2025-04-09T22:24:29","modified_gmt":"2025-04-09T21:24:29","slug":"passing-attribute-values-to-a-javascript-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/attributes\/passing-attribute-values-to-a-javascript-file","title":{"rendered":"Passing attribute values to a javascript file"},"content":{"rendered":"\n<p>You&#8217;re including a .js file but you need to provide it with values from your HTML.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">In your HTML<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script type=\"text\/javascript\" my_attribute_1='value_1' my_attribute_2='value_2' src=\"\/my-script.js\"&gt;&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">In your JavaScript file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Get attributes passed in the html script \nconst script = document.currentScript\nconst attributes = script.attributes\n\nvar my_attribute_1 = attributes.getNamedItem('my_attribute_1').value;\nalert('my_attribute_1: ' + my_attribute_1); \/\/&lt;Debug view it\n\nvar my_attribute_2 = attributes.getNamedItem('my_attribute_2').value;\nalert('my_attribute_2: ' + my_attribute_2); \/\/&lt;Debug view it<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;re including a .js file but you need to provide it with values from your HTML. In your HTML In your JavaScript file<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[337,338],"tags":[],"class_list":["post-4306","post","type-post","status-publish","format-standard","hentry","category-attributes","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=4306"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4306\/revisions"}],"predecessor-version":[{"id":4308,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4306\/revisions\/4308"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=4306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=4306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=4306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}