{"id":5422,"date":"2023-08-05T17:12:00","date_gmt":"2023-08-05T22:12:00","guid":{"rendered":"https:\/\/www.incredigeek.com\/home\/?p=5422"},"modified":"2023-08-05T14:16:40","modified_gmt":"2023-08-05T19:16:40","slug":"javascript-how-to-execute-a-function-with-variables-when-button-is-clicked","status":"publish","type":"post","link":"https:\/\/www.incredigeek.com\/home\/javascript-how-to-execute-a-function-with-variables-when-button-is-clicked\/","title":{"rendered":"JavaScript &#8211; How To Execute a Function with Variables when Button is Clicked"},"content":{"rendered":"\n<p>If we take the following code, we call the Log function without the ().  This calls the Log function every time the buttonObject is clicked.  <\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">\/\/ Find object id myButton\nlet buttonObject = document.getElementById('myButton')  \n\n\/\/ Add an event listener, and run Log function when clicked.\nbuttonObject.addEventListener('click', Log) \n\/\/ If we call Log(), it will immediately trigger the function\n\nfunction Log () {\n  console.log(\"Hello World!\")\n}<\/code><\/pre>\n\n\n\n<p>But what if we want to pass in a variable to the Log function?  We can&#8217;t run <code>Log('some text')<\/code> as the function will run before we click the object.<\/p>\n\n\n\n<p>We can however wrap the Log function inside of an anonymous function like so<\/p>\n\n\n\n<pre class=\"wp-block-code has-dark-gray-background-color has-background\"><code class=\"\">let buttonObject = document.getElementById('myButton')\n\n\/\/ Now Log() function will be run with the variable getting passed.\nbuttonObject.addEventListener('click', () => {\n  Log('Hello World!')\n})\n\nfunction Log (textVariable) {\n  console.log(textVariable)\n}<\/code><\/pre>\n\n\n\n<p>Our Log function gets triggered when the object is clicked, and the variable is passed properly.  You can swap out thy arrow function with a nameless <code>function ()<\/code> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If we take the following code, we call the Log function without the (). This calls the Log function every time the buttonObject is clicked. But what if we want to pass in a variable to the Log function? We &hellip; <a href=\"https:\/\/www.incredigeek.com\/home\/javascript-how-to-execute-a-function-with-variables-when-button-is-clicked\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[792],"tags":[1556,649,644,1500,1519,1437,1463,606],"class_list":["post-5422","post","type-post","status-publish","format-standard","hentry","category-programming","tag-anonymous-function","tag-button","tag-code","tag-function","tag-functions","tag-javascript","tag-js","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5422","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/comments?post=5422"}],"version-history":[{"count":1,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5422\/revisions"}],"predecessor-version":[{"id":5423,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/posts\/5422\/revisions\/5423"}],"wp:attachment":[{"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/media?parent=5422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/categories?post=5422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.incredigeek.com\/home\/wp-json\/wp\/v2\/tags?post=5422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}