Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Javascript not working in Wordpress / WooCommerce plugin
    text
    copied!<p>The code below is for a simple plugin that adds a new tab to the wooCommerce product page (frontend), and then adds a button to that tab. What I want it to do is popup an alert when the button is clicked. This is super simple, I know, but I created it to try and figure out why a more complicated script wasn't working. </p> <p>So far, the tab exists, and the button exists, but when I click on the button nothing happens. I have checked to make sure the script is actually being loaded, and I have confirmed that it is indeed present in the source code of the page. If I load the javascript filepath from the source code directly in my browser it displays the correct code.</p> <p>Further, if I make a simple html file with the javascript code and the button (so none of the wordpress stuff) it works perfectly well.</p> <p>Further, I have added a simple alert command directly to the html output in the php file, and this also works fine.</p> <p>This has led me to believe the issue relates to the IDs, but I don't know what to do differently.</p> <p>Here is the php file (minus the plugin header for clarity):</p> <pre><code>&lt;?php //runs addScripts add_action( 'wp_enqueue_scripts', 'addScripts' ); // enqueues jquery and test1.js files. Inspecting html shows these are loaded.test1.js is loaded in footer. function addScripts(){ wp_enqueue_script( 'jquery', plugins_url( 'jquery.js', __FILE__ )); wp_enqueue_script( 'test1', plugins_url( 'test1.js', __FILE__ ),array('jquery'),1,true); } // creates new tab in woocommerce add_filter( 'woocommerce_product_tabs', 'woocommerce_product_custom_tab' ); // function defining test_tab function woocommerce_product_custom_tab($tabs) { $tabs['test_tab'] = array( 'title' =&gt; __( 'New Product Tab', 'woocommerce' ), 'priority' =&gt; 50, 'callback' =&gt; 'woo_new_product_tab_content' ); return $tabs; } // content of new tab, and where the javascript is supposed to work. function woo_new_product_tab_content() { echo &lt;&lt;&lt;END &lt;button id="mybutton" type="button"&gt;Click Me&lt;/button&gt; &lt;script type="text/javascript" src="test1.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; &lt;!--test1();//--&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; alert('ALERT!') &lt;/script&gt; END; } ?&gt; </code></pre> <p>And here is the test1.js containing the javascript function:</p> <pre><code>function test1(){ $( "#mybutton" ).click(function( event ) { alert ('Hello') }); } </code></pre> <p>Is it possible that #mybutton in test1.js isn't leading the function to the 'mybutton' button in the php/html?</p> <p>Thanks for your help! </p> <hr> <p>Update: I now have a functioning button using the following in the php echo:</p> <pre><code>&lt;script type="text/javascript"&gt; jQuery(document).ready(function($) { $( "#mybutton" ).click(function( event ) { alert ('Hello')}); }); &lt;/script&gt; </code></pre> <p>But the following doesn't work:</p> <pre><code>&lt;script type="text/javascript"&gt; jQuery(document).ready(function($) { test1(); }); &lt;/script&gt; </code></pre> <p>With test1.js as it was before. </p> <hr> <p>update 2</p> <p>So whilst the answer below finally got the code working, the more complex stuff that this was a trial for was failing. I finally got it working (and this is probably not the most elegant solution) by doing the following:</p> <pre><code>add_action('wp_head', 'DPG_execute_script'); function DPG_execute_script() { ?&gt; &lt;script type="text/javascript"&gt; jQuery(document).ready(function($) { // javascript goes here. }); &lt;/script&gt; &lt;?php } </code></pre> <p>I couldn't get it to work with echo &lt;&lt;
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload