{"id":3837041,"date":"2013-06-12T19:36:41","date_gmt":"2013-06-12T19:36:41","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/javascript-referenceerror-variable-is-not-defined\/"},"modified":"2016-08-21T01:57:03","modified_gmt":"2016-08-21T01:57:03","slug":"javascript-referenceerror-variable-is-not-defined","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/javascript-referenceerror-variable-is-not-defined\/","title":{"rendered":"JavaScript &#8211; ReferenceError: Variable is not defined"},"content":{"rendered":"<p>Hi,<\/p>\n<p>I&#8217;m using <a href=\"http:\/\/themeforest.net\/item\/gentle-responsive-portfolio-wp-theme-retina-ready\/3349899\">this Theme<\/a>.<\/p>\n<p>I embedded a video in that top edge-to-edge headerbox. The video is on autostart and now I want to add a custom mute button for it. So since I&#8217;m far from being a JS-genius, I got myself some code from the web and created a plugin, which looks like this:<\/p>\n<pre><code>&lt;?php\n\/*\nPlugin Name: LaLi Mute Button\nPlugin URI:\nDescription: Adds a mute button to the main page\nVersion: 1.1\nAuthor: Treppenmanni\nAuthor URI:\nLicense:\n*\/\n\nfunction lali_mute_button() {\n\n\twp_register_script( &#039;lali_mutebutton&#039;, get_template_directory_uri() . &#039;\/js\/mutebutton.js&quot;&gt;&#039;, &#039;&#039;, null,&#039;&#039;  );\n\n\tif ( is_front_page() ) {\n\t\twp_enqueue_script( &#039;lali_mutebutton&#039; );\n\t}\n}\n\nadd_action( &#039;wp_enqueue_scripts&#039;, &#039;lali_mute_button&#039; );\n?&gt;<\/code><\/pre>\n<p>The JS-file:<\/p>\n<pre><code>\/\/ Connecting vars\nwindow.onload = function() {\n\n  \/\/ Video\n  var video = document.getElementById(&quot;video&quot;);\n\n  \/\/ Buttons\n  var mutebutton = document.getElementById(&quot;mute&quot;);\n\n}\n\n\/\/ Event listener for the mute button\n\nmutebutton.addEventListener(&quot;click&quot;, function() {\n  if (video.muted == false) {\n    \/\/ Mute the video\n    video.muted = true;\n\n    \/\/ Update the button text\n    muteButton.innerHTML = &quot;Unmute&quot;;\n  } else {\n    \/\/ Unmute the video\n    video.muted = false;\n\n    \/\/ Update the button text\n    mutebutton.innerHTML = &quot;Mute&quot;;\n  }\n});<\/code><\/pre>\n<p>HTML on the Main site:<\/p>\n<pre><code>&lt;div id=&quot;video-container&quot;&gt;\n  &lt;!-- Video --&gt;\n  &lt;video name &quot;reel&quot; id=&quot;video&quot; width=&quot;100%&quot;&gt;\n   &lt;source src=&quot;video\/Showreel.iphone.mp4&quot;  type=&quot;video\/mp4; codecs=avc1.42E01E,mp4a.40.2&quot;&gt;\n  &lt;source src=&quot;video\/Showreel.webmsd.webm&quot; type=&quot;video\/webm; codecs=vp8,vorbis&quot;&gt;\n  &lt;source src=&quot;video\/Showreel.oggtheora.ogv&quot;  type=&quot;video\/ogg; codecs=theora,vorbis&quot;&gt;\n  &lt;\/video&gt;\n  &lt;!-- Video Controls --&gt;\n  &lt;div id=&quot;video-controls&quot;&gt;\n    &lt;button type=&quot;button&quot; id=&quot;mute&quot;&gt;Mute&lt;\/button&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<p>Before that, I stumbled over this register-enqueue-thing and managed to get it running (took it&#8217;s time). The Plugin loads the script, the getElementByIds give back the correct values, but the button does nothing. Firebug gives me an error message: <strong>ReferenceError: mutebutton is not defined.<\/strong> Seems as if the value of the variable instantly gets forgotten after defining. Appreciate the help.<\/p>\n<p>Treppenmanfred<\/p>\n","protected":false},"template":"","class_list":["post-3837041","topic","type-topic","status-closed","hentry","topic-tag-getelementbyid","topic-tag-javascript","topic-tag-variable","topic-tag-wp_enqueue"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3837041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/3837041\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=3837041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}